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
|
|
|
|
2022-08-01 19:18:25 +02:00
|
|
|
seqmap.Initialize([]uint32{1337, 42, 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:25 +02:00
|
|
|
assert.Equal(42, 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:25 +02:00
|
|
|
assert.Equal(42, 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-08-01 19:18:25 +02:00
|
|
|
uid, found = seqmap.Get(1)
|
|
|
|
assert.Equal(1107, int(uid))
|
|
|
|
|
|
|
|
// Repeated puts of the same UID shouldn't change the size
|
|
|
|
seqmap.Put(1231)
|
|
|
|
assert.Equal(3, seqmap.Size())
|
|
|
|
seqmap.Put(1231)
|
|
|
|
assert.Equal(3, seqmap.Size())
|
|
|
|
|
|
|
|
uid, found = seqmap.Get(2)
|
|
|
|
assert.Equal(1231, int(uid))
|
|
|
|
|
2022-06-20 21:49:05 +02:00
|
|
|
_, found = seqmap.Pop(1)
|
2022-08-01 19:18:23 +02:00
|
|
|
assert.Equal(true, found)
|
2022-08-01 19:18:25 +02:00
|
|
|
assert.Equal(2, seqmap.Size())
|
2022-06-20 21:49:05 +02:00
|
|
|
|
2022-08-01 19:18:25 +02:00
|
|
|
seqmap.Initialize(nil)
|
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()
|
2022-08-01 19:18:25 +02:00
|
|
|
seqmap.Initialize([]uint32{42, 1337})
|
2022-06-20 21:49:05 +02:00
|
|
|
}()
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2022-08-01 19:18:25 +02:00
|
|
|
for _, found := seqmap.Pop(1); !found; _, found = seqmap.Pop(1) {
|
2022-06-20 21:49:05 +02:00
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2022-08-01 19:18:25 +02:00
|
|
|
for _, found := seqmap.Pop(1); !found; _, found = seqmap.Pop(1) {
|
2022-06-20 21:49:05 +02:00
|
|
|
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
|
|
|
}
|