make tests pass with "go test -count n" with n > 1

by closing initialized resources during tests.
This commit is contained in:
Mechiel Lukkien
2024-06-10 18:18:20 +02:00
parent dde2258f69
commit f56b04805b
9 changed files with 50 additions and 40 deletions

View File

@ -230,6 +230,7 @@ func TestAccount(t *testing.T) {
err = queue.Init() // For DB.
tcheck(t, err, "queue init")
defer queue.Shutdown()
account, _, _, _ := api.Account(ctx)
@ -250,6 +251,9 @@ func TestAccount(t *testing.T) {
api.AccountSaveFullName(ctx, account.FullName)
go ImportManage()
defer func() {
importers.Stop <- struct{}{}
}()
// Import mbox/maildir tgz/zip.
testImport := func(filename string, expect int) {

View File

@ -57,11 +57,13 @@ var importers = struct {
Unregister chan *importListener
Events chan importEvent
Abort chan importAbortRequest
Stop chan struct{}
}{
make(chan *importListener, 1),
make(chan *importListener, 1),
make(chan importEvent),
make(chan importAbortRequest),
make(chan struct{}),
}
// ImportManage should be run as a goroutine, it manages imports of mboxes/maildirs, propagating progress over SSE connections.
@ -89,38 +91,38 @@ func ImportManage() {
select {
case l := <-importers.Register:
// If we have state, send it so the client is up to date.
if s, ok := imports[l.Token]; ok {
l.Register <- true
s.Listeners[l] = struct{}{}
s, ok := imports[l.Token]
l.Register <- ok
if !ok {
break
}
s.Listeners[l] = struct{}{}
sendEvent := func(kind string, v any) {
buf, err := json.Marshal(v)
if err != nil {
log.Errorx("marshal event", err, slog.String("kind", kind), slog.Any("event", v))
return
}
ssemsg := fmt.Sprintf("event: %s\ndata: %s\n\n", kind, buf)
sendEvent := func(kind string, v any) {
buf, err := json.Marshal(v)
if err != nil {
log.Errorx("marshal event", err, slog.String("kind", kind), slog.Any("event", v))
return
}
ssemsg := fmt.Sprintf("event: %s\ndata: %s\n\n", kind, buf)
select {
case l.Events <- importEvent{kind, []byte(ssemsg), nil, nil}:
default:
log.Debug("dropped initial import event to slow consumer")
}
select {
case l.Events <- importEvent{kind, []byte(ssemsg), nil, nil}:
default:
log.Debug("dropped initial import event to slow consumer")
}
}
for m, c := range s.MailboxCounts {
sendEvent("count", importCount{m, c})
}
for _, p := range s.Problems {
sendEvent("problem", importProblem{p})
}
if s.Done != nil {
sendEvent("done", importDone{})
} else if s.Aborted != nil {
sendEvent("aborted", importAborted{})
}
} else {
l.Register <- false
for m, c := range s.MailboxCounts {
sendEvent("count", importCount{m, c})
}
for _, p := range s.Problems {
sendEvent("problem", importProblem{p})
}
if s.Done != nil {
sendEvent("done", importDone{})
} else if s.Aborted != nil {
sendEvent("aborted", importAborted{})
}
case l := <-importers.Unregister:
@ -172,6 +174,9 @@ func ImportManage() {
}
s.Cancel()
a.Response <- nil
case <-importers.Stop:
return
}
// Cleanup old state.