File: parallel_test.go

package info (click to toggle)
golang-github-protonmail-gluon 0.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,020 kB
  • sloc: sh: 55; makefile: 5
file content (95 lines) | stat: -rw-r--r-- 2,456 bytes parent folder | download
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
86
87
88
89
90
91
92
93
94
95
package tests

import (
	"math/rand"
	"testing"

	"github.com/ProtonMail/gluon/imap"
	"github.com/bradenaw/juniper/xslices"
	"github.com/emersion/go-imap/client"
	"github.com/stretchr/testify/require"
)

func TestSelectWhileSyncing(t *testing.T) {
	runOneToOneTestClientWithAuth(t, defaultServerOptions(t), func(client *client.Client, s *testSession) {
		// Define some mailbox names.
		mailboxNames := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}

		// Collect the mailbox IDs as they're created.
		mailboxIDs := xslices.Map(mailboxNames, func(name string) imap.MailboxID {
			return s.mailboxCreated("user", []string{name})
		})

		stopCh := make(chan struct{})
		doneCh := make(chan struct{})

		// Append a bunch of messages.
		go func() {
			defer close(doneCh)

			for {
				select {
				case <-stopCh:
					return

				default:
					s.messageCreatedFromFile("user", mailboxIDs[0], `testdata/multipart-mixed.eml`)
				}
			}
		}()

		// Select a bunch of mailboxes.
		for i := 0; i < 100; i++ {
			mboxName := mailboxNames[rand.Int()%len(mailboxNames)] //nolint:gosec
			_, err := client.Select(mboxName, false)
			require.NoError(t, err)

			_, err = client.Select("INBOX", false)
			require.NoError(t, err)
		}

		// Stop appending.
		close(stopCh)

		// Wait for appending to finish.
		<-doneCh
	})
}

func TestTwoFetchesAtOnce(t *testing.T) {
	runOneToOneTestWithAuth(t, defaultServerOptions(t), func(c *testConnection, s *testSession) {
		mboxID := s.mailboxCreated("user", []string{"mbox"})

		for i := 0; i < 1000; i++ {
			s.messageCreatedFromFile("user", mboxID, `testdata/multipart-mixed.eml`, `\Seen`)
		}

		c.C("A006 select mbox")
		c.Se("A006 OK [READ-WRITE] SELECT")

		// Do some fetches in parallel.
		c.C(`A005 FETCH 1:200 (BODY[TEXT])`)
		c.C(`A006 FETCH 201:400 (BODY[TEXT])`)
		c.C(`A007 FETCH 401:600 (BODY[TEXT])`)
		c.C(`A008 FETCH 601:800 (BODY[TEXT])`)
		c.C(`A009 FETCH 801:1000 (BODY[TEXT])`)

		// The fetch commands will complete eventually; who knows which will be processed first.
		// TODO: Also check the untagged FETCH responses.
		c.Sxe(
			`A005 OK command completed in .*`,
			`A006 OK command completed in .*`,
			`A007 OK command completed in .*`,
			`A008 OK command completed in .*`,
			`A009 OK command completed in .*`,
		)

		// We should then be able to logout fine.
		c.C("A010 logout")
		c.S("* BYE")
		c.OK("A010")

		// Logging out should close the connection.
		c.expectClosed()
	})
}