diff options
author | Tim Culverhouse <tim@timculverhouse.com> | 2022-08-01 12:18:25 -0500 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-08-03 22:37:13 +0200 |
commit | fdfec2c07a8d37f2017511fcca6a060c4e6e8eac (patch) | |
tree | 2bca81a6f3f5bb25da066bbb046f9badcc76f484 /worker/imap/seqmap_test.go | |
parent | 9630d9d281e6dc2ea20f7def3531ee3fcf8c6c47 (diff) | |
download | aerc-fdfec2c07a8d37f2017511fcca6a060c4e6e8eac.tar.gz |
seqmap: refactor seqmap to use slice instead of map
The imap worker's seqmap is represented as a map of sequence number to
UID. This presents a problem when expunging group of messages from the
mailbox: each individual expunge decrements the sequence numbers by 1
(for every sequence number greater than the expunged). This requires a
looping around the map to update the keys. The use of a map also
requires that both the sequence number and the UID of a message be known
in order to insert it into the map. This is only discovered by fetching
individual message body parts (flags, headers, etc), leaving the seqmap
to be empty until we have fetched information about each message. In
certain instances (if a mailbox has recently been loaded), all
information is loaded in memory and no new information is fetched -
leaving the seqmap empty and the UI out of sync with the worker.
Refactor the seqmap as a slice, so that any expunge automatically
decrements the rest of the sequences.
Use the results of FetchDirectoryContents or FetchDirectoryThreaded to
initialize the seqmap with all discovered UIDs. Sort the UIDs in
ascending order: IMAP specification requires that sequence numbers start
at 1 increase in order of ascending UID.
Add individual messages to the map if they come via a MessageUpdate and
have a sequence number larger than our slice.
Update seqmap tests with new logic.
Reference: https://datatracker.ietf.org/doc/html/rfc3501#section-2.3.1.2
Fixes: https://todo.sr.ht/~rjarry/aerc/69
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'worker/imap/seqmap_test.go')
-rw-r--r-- | worker/imap/seqmap_test.go | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/worker/imap/seqmap_test.go b/worker/imap/seqmap_test.go index 70677218..42c06f81 100644 --- a/worker/imap/seqmap_test.go +++ b/worker/imap/seqmap_test.go @@ -22,56 +22,57 @@ func TestSeqMap(t *testing.T) { _, found = seqmap.Pop(0) assert.Equal(false, found) - seqmap.Put(1, 1337) - seqmap.Put(2, 42) - seqmap.Put(3, 1107) + seqmap.Initialize([]uint32{1337, 42, 1107}) assert.Equal(3, seqmap.Size()) _, found = seqmap.Pop(0) assert.Equal(false, found) uid, found = seqmap.Get(1) - assert.Equal(1337, int(uid)) + assert.Equal(42, int(uid)) assert.Equal(true, found) uid, found = seqmap.Pop(1) - assert.Equal(1337, int(uid)) + assert.Equal(42, int(uid)) assert.Equal(true, found) assert.Equal(2, seqmap.Size()) - // Repop the same seqnum should work because of the syncing + 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(1, seqmap.Size()) - - // sync means we already have a 1. This is replacing that UID so the size - // shouldn't increase - seqmap.Put(1, 7331) - assert.Equal(1, seqmap.Size()) + assert.Equal(2, seqmap.Size()) - seqmap.Clear() + seqmap.Initialize(nil) assert.Equal(0, seqmap.Size()) 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) + seqmap.Initialize([]uint32{42, 1337}) }() wg.Add(1) go func() { defer wg.Done() - for _, found := seqmap.Pop(43); !found; _, found = seqmap.Pop(43) { + 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(42); !found; _, found = seqmap.Pop(42) { + for _, found := seqmap.Pop(1); !found; _, found = seqmap.Pop(1) { time.Sleep(1 * time.Millisecond) } }() |