forked from jjeffery/stomp
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathsubscription_test.go
129 lines (107 loc) · 3.41 KB
/
subscription_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package stomp
import (
"github.com/go-stomp/stomp/v3/frame"
"github.com/go-stomp/stomp/v3/testutil"
"sync"
"time"
. "gopkg.in/check.v1"
)
func (s *StompSuite) Test_successful_unsubscribe_with_receipt_timeout(c *C) {
resetId()
wg, fc1, fc2 := runFakeConn(c,
assertConnectFrame,
sendConnectedFrame,
assertSubscribeFrame,
assertUnsubscribeFrame,
)
defer fc1.Close()
defer fc2.Close()
client, err := Connect(fc1, ConnOpt.UnsubscribeReceiptTimeout(1*time.Second))
c.Assert(err, IsNil)
c.Assert(client, NotNil)
sub, err := client.Subscribe("/queue/test", AckAuto)
c.Assert(err, IsNil)
c.Assert(sub, NotNil)
err = sub.Unsubscribe()
c.Assert(err, Equals, &ErrUnsubscribeReceiptTimeout)
wg.Wait()
}
func (s *StompSuite) Test_successful_unsubscribe_no_timeout(c *C) {
resetId()
wg, fc1, fc2 := runFakeConn(c,
assertConnectFrame,
sendConnectedFrame,
assertSubscribeFrame,
assertUnsubscribeFrame,
sendReceiptFrame(3),
)
defer fc1.Close()
defer fc2.Close()
client, err := Connect(fc1)
c.Assert(err, IsNil)
c.Assert(client, NotNil)
sub, err := client.Subscribe("/queue/test", AckAuto)
c.Assert(err, IsNil)
c.Assert(sub, NotNil)
err = sub.Unsubscribe()
c.Assert(err, IsNil)
wg.Wait()
}
// serverOperation is a function that performs a server operation that either reads or writes a frame and returns it.
type serverOperation func(c *C, reader *frame.Reader, writer *frame.Writer, previousFrames []*frame.Frame) (*frame.Frame, error)
func runFakeConn(c *C, operations ...serverOperation) (*sync.WaitGroup, *testutil.FakeConn, *testutil.FakeConn) {
client, server := testutil.NewFakeConn(c)
wg := &sync.WaitGroup{}
go func() {
wg.Add(1)
defer wg.Done()
reader := frame.NewReader(server)
writer := frame.NewWriter(server)
frames := make([]*frame.Frame, 0)
for _, operation := range operations {
frame, err := operation(c, reader, writer, frames)
frames = append(frames, frame)
if err != nil {
c.Errorf("error in server operation: %v", err)
return
}
}
}()
return wg, client, server
}
func assertConnectFrame(c *C, reader *frame.Reader, _ *frame.Writer, _ []*frame.Frame) (*frame.Frame, error) {
f, err := reader.Read()
c.Assert(err, IsNil)
c.Assert(f.Command, Equals, frame.CONNECT)
return f, err
}
func sendConnectedFrame(c *C, _ *frame.Reader, writer *frame.Writer, _ []*frame.Frame) (*frame.Frame, error) {
f := frame.New(frame.CONNECTED)
err := writer.Write(f)
c.Assert(err, IsNil)
return f, err
}
func assertSubscribeFrame(c *C, reader *frame.Reader, _ *frame.Writer, _ []*frame.Frame) (*frame.Frame, error) {
f, err := reader.Read()
c.Assert(err, IsNil)
c.Assert(f.Command, Equals, frame.SUBSCRIBE)
return f, err
}
func assertUnsubscribeFrame(c *C, reader *frame.Reader, _ *frame.Writer, _ []*frame.Frame) (*frame.Frame, error) {
f, err := reader.Read()
c.Assert(err, IsNil)
c.Assert(f.Command, Equals, frame.UNSUBSCRIBE)
return f, err
}
// sendReceiptFrame returns a server operation that writes a RECEIPT frame to the writer based on the id of the previous frame
func sendReceiptFrame(frameId int) serverOperation {
return func(c *C, _ *frame.Reader, writer *frame.Writer, previousFrames []*frame.Frame) (*frame.Frame, error) {
f := frame.New(frame.RECEIPT)
previousFrame := previousFrames[frameId]
c.Assert(previousFrame, NotNil)
f.Header.Set(frame.ReceiptId, previousFrame.Header.Get(frame.Id))
err := writer.Write(f)
c.Assert(err, IsNil)
return f, err
}
}