aerc/worker/imap/seqmap_test.go
Tim Culverhouse fb5558da81 seqmap: sync seqNum to uid after expunge
This patch updates the seqNums after an Expunge operation. When an
expunge operation occurs, the seqNum of the deleted message is reported.
The Imap spec [0] states that an immediate decrement of all seqnums greater
than the deleted occurs, even before the next reporting of an expunge
update.

[0]: https://datatracker.ietf.org/doc/html/rfc3501#section-7.4.1

Fixes: https://todo.sr.ht/~rjarry/aerc/61
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Signed-off-by: Robin Jarry <robin@jarry.cc>
2022-07-24 23:06:10 +02:00

81 lines
1.6 KiB
Go

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)
// Repop the same seqnum should work because of the syncing
_, found = seqmap.Pop(1)
assert.Equal(found, true)
assert.Equal(seqmap.Size(), 1)
// sync means we already have a 1. This is replacing that UID so the size
// shouldn't increase
seqmap.Put(1, 7331)
assert.Equal(seqmap.Size(), 1)
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)
}