aboutsummaryrefslogtreecommitdiffstats
path: root/worker/imap/seqmap_test.go
blob: 5d6cf796ec7f70383a6a7d7b0bb260d91f5560df (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
79
80
81
82
83
84
85
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(0, seqmap.Size())

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

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

	uids := []uint32{1337, 42, 1107}
	seqmap.Initialize(uids)
	assert.Equal(3, seqmap.Size())
	// Original list should remain unsorted
	assert.Equal([]uint32{1337, 42, 1107}, uids)

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

	uid, found = seqmap.Get(1)
	assert.Equal(42, int(uid))
	assert.Equal(true, found)

	uid, found = seqmap.Pop(1)
	assert.Equal(42, int(uid))
	assert.Equal(true, found)
	assert.Equal(2, seqmap.Size())

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

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

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

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		seqmap.Initialize([]uint32{42, 1337})
	}()
	wg.Add(1)
	go func() {
		defer wg.Done()
		for _, found := seqmap.Pop(1); !found; _, found = seqmap.Pop(1) {
			time.Sleep(1 * time.Millisecond)
		}
	}()
	wg.Add(1)
	go func() {
		defer wg.Done()
		for _, found := seqmap.Pop(1); !found; _, found = seqmap.Pop(1) {
			time.Sleep(1 * time.Millisecond)
		}
	}()
	wg.Wait()

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