2022-06-20 21:49:05 +02:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSeqMap(t *testing.T) {
|
|
|
|
var seqmap SeqMap
|
|
|
|
var uid uint32
|
|
|
|
var found bool
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-08-01 19:18:23 +02:00
|
|
|
assert.Equal(0, seqmap.Size())
|
2022-06-20 21:49:05 +02:00
|
|
|
|
|
|
|
_, found = seqmap.Get(42)
|
2022-08-01 19:18:23 +02:00
|
|
|
assert.Equal(false, found)
|
2022-06-20 21:49:05 +02:00
|
|
|
|
|
|
|
_, found = seqmap.Pop(0)
|
2022-08-01 19:18:23 +02:00
|
|
|
assert.Equal(false, found)
|
2022-06-20 21:49:05 +02:00
|
|
|
|
|
|
|
seqmap.Put(1, 1337)
|
|
|
|
seqmap.Put(2, 42)
|
|
|
|
seqmap.Put(3, 1107)
|
2022-08-01 19:18:23 +02:00
|
|
|
assert.Equal(3, seqmap.Size())
|
2022-06-20 21:49:05 +02:00
|
|
|
|
|
|
|
_, found = seqmap.Pop(0)
|
2022-08-01 19:18:23 +02:00
|
|
|
assert.Equal(false, found)
|
2022-06-20 21:49:05 +02:00
|
|
|
|
|
|
|
uid, found = seqmap.Get(1)
|
2022-08-01 19:18:24 +02:00
|
|
|
assert.Equal(1337, int(uid))
|
2022-08-01 19:18:23 +02:00
|
|
|
assert.Equal(true, found)
|
2022-06-20 21:49:05 +02:00
|
|
|
|
|
|
|
uid, found = seqmap.Pop(1)
|
2022-08-01 19:18:24 +02:00
|
|
|
assert.Equal(1337, int(uid))
|
2022-08-01 19:18:23 +02:00
|
|
|
assert.Equal(true, found)
|
|
|
|
assert.Equal(2, seqmap.Size())
|
2022-06-20 21:49:05 +02:00
|
|
|
|
2022-07-24 17:13:43 +02:00
|
|
|
// Repop the same seqnum should work because of the syncing
|
2022-06-20 21:49:05 +02:00
|
|
|
_, found = seqmap.Pop(1)
|
2022-08-01 19:18:23 +02:00
|
|
|
assert.Equal(true, found)
|
|
|
|
assert.Equal(1, seqmap.Size())
|
2022-06-20 21:49:05 +02:00
|
|
|
|
2022-07-24 17:13:43 +02:00
|
|
|
// sync means we already have a 1. This is replacing that UID so the size
|
|
|
|
// shouldn't increase
|
2022-06-20 21:49:05 +02:00
|
|
|
seqmap.Put(1, 7331)
|
2022-08-01 19:18:23 +02:00
|
|
|
assert.Equal(1, seqmap.Size())
|
2022-06-20 21:49:05 +02:00
|
|
|
|
|
|
|
seqmap.Clear()
|
2022-08-01 19:18:23 +02:00
|
|
|
assert.Equal(0, seqmap.Size())
|
2022-06-20 21:49:05 +02:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
seqmap.Put(42, 1337)
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
seqmap.Put(43, 1107)
|
|
|
|
}()
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for _, found := seqmap.Pop(43); !found; _, found = seqmap.Pop(43) {
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for _, found := seqmap.Pop(42); !found; _, found = seqmap.Pop(42) {
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
|
2022-08-01 19:18:23 +02:00
|
|
|
assert.Equal(0, seqmap.Size())
|
2022-06-20 21:49:05 +02:00
|
|
|
}
|