Repository navigation
Expand file tree
/
Copy pathmsg_seq_test.go
More file actions
104 lines (87 loc) · 1.94 KB
/
Copy pathmsg_seq_test.go
File metadata and controls
104 lines (87 loc) · 1.94 KB
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
96
97
98
99
100
101
102
103
104
package mtproto
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/gotd/td/bin"
"github.com/gotd/td/tg"
)
func TestMsgSeq(t *testing.T) {
type record struct {
msgID int64
seqNo int32
}
var (
records []record
mux sync.Mutex
)
const (
workers = 32
requestsPerWorker = 200
)
client := newTestClient(func(msgID int64, seqNo int32, body bin.Encoder) (bin.Encoder, error) {
mux.Lock()
records = append(records, record{msgID, seqNo})
mux.Unlock()
return &tg.Config{}, nil
})
{
var wg sync.WaitGroup
wg.Add(workers)
for i := 0; i < workers; i++ {
go func(t *testing.T) {
defer wg.Done()
for i := 0; i < requestsPerWorker; i++ {
err := client.Invoke(context.Background(), &tg.Config{}, &tg.Config{})
require.NoError(t, err)
}
}(t)
}
wg.Wait()
}
var received []record
less := func(msgID int64, f func(r record)) {
for _, recv := range received {
if recv.msgID < msgID {
f(recv)
}
}
}
greater := func(msgID int64, f func(r record)) {
for _, recv := range received {
if recv.msgID > msgID {
f(recv)
}
}
}
contains := func(msgID int64) bool {
for _, rec := range received {
if rec.msgID == msgID {
return true
}
}
return false
}
for _, current := range records {
if contains(current.msgID) {
// Ignore duplicates.
continue
}
less(current.msgID, func(less record) {
// The server has already received a message with a lower msg_id
// but with either a higher or an equal and odd seqno.
if less.seqNo >= current.seqNo && less.seqNo%2 == 1 {
t.Fatal("seqNo too low")
}
})
greater(current.msgID, func(greater record) {
// Similarly, there is a message with a higher msg_id
// but with either a lower or an equal and odd seqno.
if greater.seqNo <= current.seqNo && greater.seqNo%2 == 1 {
t.Fatal("seqNo too high")
}
})
received = append(received, current)
}
}