In tests, make initializing store/, its switchboard and an account more consistent.

Initialize store and switchboard first, then open account, and close in reverse
order.

Replace all "CheckClosed" calls with "WaitClosed", future changings will keep
an account reference open for a bit after the last regular close, so we can't
know that an account should be closed during tests.

Remove one parameter from the (still too long) "start test server" function in
imapserver testing code.
This commit is contained in:
Mechiel Lukkien
2025-03-15 11:15:23 +01:00
parent eadbda027c
commit c4255a96f8
16 changed files with 69 additions and 62 deletions

View File

@ -965,8 +965,8 @@ var InitialUIDValidity = func() uint32 {
}
var openAccounts = struct {
names map[string]*Account
sync.Mutex
names map[string]*Account
}{
names: map[string]*Account{},
}
@ -1029,6 +1029,7 @@ func OpenAccount(log mlog.Log, name string, checkLoginDisabled bool) (*Account,
}
// openAccount opens an existing account, or creates it if it is missing.
// Called with openAccounts lock held.
func openAccount(log mlog.Log, name string) (a *Account, rerr error) {
dir := filepath.Join(mox.DataDirPath("accounts"), name)
return OpenAccountDB(log, dir, name)
@ -1582,15 +1583,6 @@ func (a *Account) WaitClosed() {
<-a.closed
}
// CheckClosed asserts that the account has a zero reference count. For use in tests.
func (a *Account) CheckClosed() {
openAccounts.Lock()
defer openAccounts.Unlock()
if a.nused != 0 {
panic(fmt.Sprintf("account still in use, %d refs", a.nused))
}
}
// Close reduces the reference count, and closes the database connection when
// it was the last user.
func (a *Account) Close() error {

View File

@ -41,6 +41,13 @@ func TestMailbox(t *testing.T) {
os.RemoveAll("../testdata/store/data")
mox.ConfigStaticPath = filepath.FromSlash("../testdata/store/mox.conf")
mox.MustLoadConfig(true, false)
err := Init(ctxbg)
tcheck(t, err, "init")
defer func() {
err := Close()
tcheck(t, err, "close")
}()
defer Switchboard()()
acc, err := OpenAccount(log, "mjl", false)
tcheck(t, err, "open account")
defer func() {
@ -48,7 +55,6 @@ func TestMailbox(t *testing.T) {
tcheck(t, err, "closing account")
acc.WaitClosed()
}()
defer Switchboard()()
msgFile, err := CreateMessageTemp(log, "account-test")
tcheck(t, err, "create temp message file")
@ -316,12 +322,20 @@ func TestNextMessageID(t *testing.T) {
os.RemoveAll("../testdata/store/data")
mox.ConfigStaticPath = filepath.FromSlash("../testdata/store/mox.conf")
mox.MustLoadConfig(true, false)
err := Init(ctxbg)
tcheck(t, err, "init")
defer func() {
err := Close()
tcheck(t, err, "close")
}()
defer Switchboard()()
// Ensure account exists.
acc, err := OpenAccount(log, "mjl", false)
tcheck(t, err, "open account")
err = acc.Close()
tcheck(t, err, "closing account")
acc.WaitClosed()
acc = nil
// Create file on disk to occupy the first Message.ID that would otherwise be used for deliveries..
@ -342,7 +356,6 @@ func TestNextMessageID(t *testing.T) {
// Open account. This should increase the next message ID.
acc, err = OpenAccount(log, "mjl", false)
tcheck(t, err, "open account")
defer Switchboard()()
// Deliver a message. It should get ID 2.
mf, err := CreateMessageTemp(log, "account-test")
@ -369,6 +382,7 @@ func TestNextMessageID(t *testing.T) {
err = acc.Close()
tcheck(t, err, "closing account")
acc.WaitClosed()
// Try again, but also create next message directory, but no file.
os.MkdirAll(filepath.Join(msgDir, "b"), 0700)

View File

@ -24,14 +24,14 @@ func TestExport(t *testing.T) {
os.RemoveAll("../testdata/store/data")
mox.ConfigStaticPath = filepath.FromSlash("../testdata/store/mox.conf")
mox.MustLoadConfig(true, false)
defer Switchboard()()
acc, err := OpenAccount(pkglog, "mjl", false)
tcheck(t, err, "open account")
defer func() {
err := acc.Close()
log.Check(err, "closing account")
acc.CheckClosed()
acc.WaitClosed()
}()
defer Switchboard()()
msgFile, err := CreateMessageTemp(pkglog, "mox-test-export")
tcheck(t, err, "create temp")

View File

@ -19,14 +19,15 @@ func TestThreadingUpgrade(t *testing.T) {
os.RemoveAll("../testdata/store/data")
mox.ConfigStaticPath = filepath.FromSlash("../testdata/store/mox.conf")
mox.MustLoadConfig(true, false)
defer Switchboard()()
acc, err := OpenAccount(log, "mjl", true)
tcheck(t, err, "open account")
defer func() {
err = acc.Close()
tcheck(t, err, "closing account")
acc.CheckClosed()
acc.WaitClosed()
}()
defer Switchboard()()
// New account already has threading. Add some messages, check the threading.
deliver := func(recv time.Time, s string, expThreadID int64) Message {
@ -120,7 +121,7 @@ func TestThreadingUpgrade(t *testing.T) {
dbpath := acc.DBPath
err = acc.Close()
tcheck(t, err, "close account")
acc.CheckClosed()
acc.WaitClosed()
// Now clear the threading upgrade, and the threading fields and close the account.
// We open the database file directly, so we don't trigger the consistency checker.