-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nutbunnies/master
added locking to mutation methods Store, DeleteOne and DeleteAll
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package storage | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/mailhog/data" | ||
) | ||
|
||
func TestStore(t *testing.T) { | ||
storage := CreateInMemory() | ||
|
||
if storage.Count() != 0 { | ||
t.Errorf("storage.Count() expected: %d, got: %d", 0, storage.Count()) | ||
} | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(25) | ||
for i := 0; i < 25; i++ { | ||
go func(i int) { | ||
msg := &data.Message{ | ||
ID: data.MessageID(i), | ||
Created: time.Now(), | ||
} | ||
storage.Store(msg) | ||
wg.Done() | ||
}(i) | ||
} | ||
wg.Wait() | ||
|
||
if storage.Count() != 25 { | ||
t.Errorf("storage.Count() expected: %d, got: %d", 25, storage.Count()) | ||
} | ||
} | ||
|
||
func TestDeleteAll(t *testing.T) { | ||
storage := CreateInMemory() | ||
|
||
if storage.Count() != 0 { | ||
t.Errorf("storage.Count() expected: %d, got: %d", 0, storage.Count()) | ||
} | ||
|
||
for i := 0; i < 25; i++ { | ||
storage.Store(&data.Message{ID: data.MessageID(i), Created: time.Now()}) | ||
} | ||
|
||
if storage.Count() != 25 { | ||
t.Errorf("storage.Count() expected: %d, got: %d", 25, storage.Count()) | ||
} | ||
|
||
storage.DeleteAll() | ||
|
||
if storage.Count() != 0 { | ||
t.Errorf("storage.Count() expected: %d, got: %d", 0, storage.Count()) | ||
} | ||
} | ||
|
||
func TestDeleteOne(t *testing.T) { | ||
storage := CreateInMemory() | ||
|
||
if storage.Count() != 0 { | ||
t.Errorf("storage.Count() expected: %d, got: %d", 0, storage.Count()) | ||
} | ||
|
||
for i := 0; i < 25; i++ { | ||
storage.Store(&data.Message{ID: data.MessageID(i), Created: time.Now()}) | ||
} | ||
|
||
storage.DeleteOne("1") | ||
|
||
if storage.Count() != 24 { | ||
t.Errorf("storage.Count() expected: %d, got: %d", 0, storage.Count()) | ||
} | ||
} |