| 1 |
package imap |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/assert" |
| 9 |
) |
| 10 |
|
| 11 |
func TestSeqMap(t *testing.T) { |
| 12 |
var seqmap SeqMap |
| 13 |
var uid uint32 |
| 14 |
var found bool |
| 15 |
assert := assert.New(t) |
| 16 |
|
| 17 |
assert.Equal(0, seqmap.Size()) |
| 18 |
|
| 19 |
_, found = seqmap.Get(42) |
| 20 |
assert.Equal(false, found) |
| 21 |
|
| 22 |
_, found = seqmap.Pop(0) |
| 23 |
assert.Equal(false, found) |
| 24 |
|
| 25 |
uids := []uint32{1337, 42, 1107} |
| 26 |
seqmap.Initialize(uids) |
| 27 |
assert.Equal(3, seqmap.Size()) |
| 28 |
// Original list should remain unsorted |
| 29 |
assert.Equal([]uint32{1337, 42, 1107}, uids) |
| 30 |
|
| 31 |
_, found = seqmap.Pop(0) |
| 32 |
assert.Equal(false, found) |
| 33 |
|
| 34 |
uid, found = seqmap.Get(1) |
| 35 |
assert.Equal(42, int(uid)) |
| 36 |
assert.Equal(true, found) |
| 37 |
|
| 38 |
uid, found = seqmap.Pop(1) |
| 39 |
assert.Equal(42, int(uid)) |
| 40 |
assert.Equal(true, found) |
| 41 |
assert.Equal(2, seqmap.Size()) |
| 42 |
|
| 43 |
uid, found = seqmap.Get(1) |
| 44 |
assert.Equal(1107, int(uid)) |
| 45 |
|
| 46 |
// Repeated puts of the same UID shouldn't change the size |
| 47 |
seqmap.Put(1231) |
| 48 |
assert.Equal(3, seqmap.Size()) |
| 49 |
seqmap.Put(1231) |
| 50 |
assert.Equal(3, seqmap.Size()) |
| 51 |
|
| 52 |
uid, found = seqmap.Get(2) |
| 53 |
assert.Equal(1231, int(uid)) |
| 54 |
|
| 55 |
_, found = seqmap.Pop(1) |
| 56 |
assert.Equal(true, found) |
| 57 |
assert.Equal(2, seqmap.Size()) |
| 58 |
|
| 59 |
seqmap.Initialize(nil) |
| 60 |
assert.Equal(0, seqmap.Size()) |
| 61 |
|
| 62 |
var wg sync.WaitGroup |
| 63 |
wg.Add(1) |
| 64 |
go func() { |
| 65 |
defer wg.Done() |
| 66 |
seqmap.Initialize([]uint32{42, 1337}) |
| 67 |
}() |
| 68 |
wg.Add(1) |
| 69 |
go func() { |
| 70 |
defer wg.Done() |
| 71 |
for _, found := seqmap.Pop(1); !found; _, found = seqmap.Pop(1) { |
| 72 |
time.Sleep(1 * time.Millisecond) |
| 73 |
} |
| 74 |
}() |
| 75 |
wg.Add(1) |
| 76 |
go func() { |
| 77 |
defer wg.Done() |
| 78 |
for _, found := seqmap.Pop(1); !found; _, found = seqmap.Pop(1) { |
| 79 |
time.Sleep(1 * time.Millisecond) |
| 80 |
} |
| 81 |
}() |
| 82 |
wg.Wait() |
| 83 |
|
| 84 |
assert.Equal(0, seqmap.Size()) |
| 85 |
|
| 86 |
// |
| 87 |
// Test snapshotting |
| 88 |
// |
| 89 |
|
| 90 |
// The sequence is [ 21, 24, 1107, 1982, 2399, 27892, 32000 ] (1 is at |
| 91 |
// position 1, 24 position 2, etc) |
| 92 |
seqmap.Initialize([]uint32{21, 42, 1107, 1982, 2390, 27892, 32000}) |
| 93 |
|
| 94 |
// Snapshot [ 21, 1107, 27892, 1234567 ]; they're all present in the |
| 95 |
// sequence except 27982 |
| 96 |
snap, min := seqmap.Snapshot([]uint32{21, 1107, 27892, 1234567}) |
| 97 |
|
| 98 |
// Verify that we did snapshot [ 1 => 21, 3=>1107, 6=>27892 ] |
| 99 |
assert.Equal(3, len(snap)) |
| 100 |
assert.Equal(uint32(1), min) |
| 101 |
assert.Equal(snap[1], uint32(21)) |
| 102 |
assert.Equal(snap[3], uint32(1107)) |
| 103 |
assert.Equal(snap[6], uint32(27892)) |
| 104 |
|
| 105 |
// Vefify that the snapshotted items have been removed from the sequence, |
| 106 |
// that therefore became [ 42, 1982, 2390, 32000 ] |
| 107 |
assert.Equal(4, seqmap.Size()) |
| 108 |
o1, _ := seqmap.Get(1) |
| 109 |
assert.Equal(uint32(42), o1) |
| 110 |
o2, _ := seqmap.Get(2) |
| 111 |
assert.Equal(uint32(1982), o2) |
| 112 |
o3, _ := seqmap.Get(3) |
| 113 |
assert.Equal(uint32(2390), o3) |
| 114 |
o4, _ := seqmap.Get(4) |
| 115 |
assert.Equal(uint32(32000), o4) |
| 116 |
} |