forked from eclipse-paho/paho.mqtt.golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_messageids_test.go
111 lines (88 loc) · 1.89 KB
/
unit_messageids_test.go
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
105
106
107
108
109
110
111
/*
* Copyright (c) 2013 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Seth Hoenig
* Allan Stockdill-Mander
* Mike Robertson
*/
package mqtt
import (
"fmt"
"testing"
"time"
)
type DummyToken struct{}
func (d *DummyToken) Wait() bool {
return true
}
func (d *DummyToken) WaitTimeout(t time.Duration) bool {
return true
}
func (d *DummyToken) flowComplete() {}
func (d *DummyToken) Error() error {
return nil
}
func Test_getID(t *testing.T) {
mids := &messageIds{index: make(map[uint16]Token)}
i1 := mids.getID(&DummyToken{})
if i1 != 1 {
t.Fatalf("i1 was wrong: %v", i1)
}
i2 := mids.getID(&DummyToken{})
if i2 != 2 {
t.Fatalf("i2 was wrong: %v", i2)
}
for i := uint16(3); i < 100; i++ {
id := mids.getID(&DummyToken{})
if id != i {
t.Fatalf("id was wrong expected %v got %v", i, id)
}
}
}
func Test_freeID(t *testing.T) {
mids := &messageIds{index: make(map[uint16]Token)}
i1 := mids.getID(&DummyToken{})
mids.freeID(i1)
if i1 != 1 {
t.Fatalf("i1 was wrong: %v", i1)
}
i2 := mids.getID(&DummyToken{})
fmt.Printf("i2: %v\n", i2)
}
func Test_messageids_mix(t *testing.T) {
mids := &messageIds{index: make(map[uint16]Token)}
done := make(chan bool)
a := make(chan uint16, 3)
b := make(chan uint16, 20)
c := make(chan uint16, 100)
go func() {
for i := 0; i < 10000; i++ {
a <- mids.getID(&DummyToken{})
mids.freeID(<-b)
}
done <- true
}()
go func() {
for i := 0; i < 10000; i++ {
b <- mids.getID(&DummyToken{})
mids.freeID(<-c)
}
done <- true
}()
go func() {
for i := 0; i < 10000; i++ {
c <- mids.getID(&DummyToken{})
mids.freeID(<-a)
}
done <- true
}()
<-done
<-done
<-done
}