aboutsummaryrefslogtreecommitdiffstats
path: root/worker/imap/seqmap_test.go
blob: 0ee07385b4684bde5a0fae92abcdee21fb93266f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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)

	assert.Equal(seqmap.Size(), 0)

	_, found = seqmap.Get(42)
	assert.Equal(found, false)

	_, found = seqmap.Pop(0)
	assert.Equal(found, false)

	seqmap.Put(1, 1337)
	seqmap.Put(2, 42)
	seqmap.Put(3, 1107)
	assert.Equal(seqmap.Size(), 3)

	_, found = seqmap.Pop(0)
	assert.Equal(found, false)

	uid, found = seqmap.Get(1)
	assert.Equal(uid, uint32(1337))
	assert.Equal(found, true)

	uid, found = seqmap.Pop(1)
	assert.Equal(uid, uint32(1337))
	assert.Equal(found, true)
	assert.Equal(seqmap.Size(), 2)

	_, found = seqmap.Pop(1)
	assert.Equal(found, false)
	assert.Equal(seqmap.Size(), 2)

	seqmap.Put(1, 7331)
	assert.Equal(seqmap.Size(), 3)

	seqmap.Clear()
	assert.Equal(seqmap.Size(), 0)

	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()

	assert.Equal(seqmap.Size(), 0)
}