This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel_writer_test.go
163 lines (131 loc) · 4.97 KB
/
channel_writer_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package rabbitmq
import (
"errors"
"testing"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
"github.com/smartystreets/messaging/v2"
"github.com/streadway/amqp"
)
func TestChannelWriterFixture(t *testing.T) {
gunit.Run(new(ChannelWriterFixture), t)
}
type ChannelWriterFixture struct {
*gunit.Fixture
writer *ChannelWriter
controller *FakeWriterController
}
func (this *ChannelWriterFixture) Setup() {
this.controller = newFakeWriterController()
this.writer = newWriter(this.controller)
}
///////////////////////////////////////////////////////////////
func (this *ChannelWriterFixture) TestDispatchIsWrittenToChannel() {
dispatch := messaging.Dispatch{
Destination: "destination",
Partition: "partition",
Payload: []byte{1, 2, 3, 4, 5},
}
err := this.writer.Write(dispatch)
this.So(err, should.BeNil)
this.So(this.controller.channel.exchange, should.Equal, dispatch.Destination)
this.So(this.controller.channel.partition, should.Equal, dispatch.Partition)
this.So(this.controller.channel.dispatch.Body, should.Resemble, dispatch.Payload)
this.So(this.controller.channel.transactional, should.BeFalse)
}
///////////////////////////////////////////////////////////////
func (this *ChannelWriterFixture) TestChannelCannotBeObtained() {
this.controller.channel = nil
err := this.writer.Write(messaging.Dispatch{})
this.So(err, should.NotBeNil)
}
///////////////////////////////////////////////////////////////
func (this *ChannelWriterFixture) TestFailedChannelClosed() {
this.controller.channel.err = errors.New("channel failed")
err := this.writer.Write(messaging.Dispatch{})
this.So(err, should.Equal, this.controller.channel.err)
this.So(this.controller.channel.closed, should.Equal, 1)
this.So(this.writer.channel, should.BeNil)
}
///////////////////////////////////////////////////////////////
func (this *ChannelWriterFixture) TestCloseWriter() {
this.writer.Close()
this.So(this.writer.closed, should.BeTrue)
this.So(this.writer.Write(messaging.Dispatch{}), should.Equal, messaging.ErrWriterClosed)
}
///////////////////////////////////////////////////////////////
type FakeWriterController struct {
channel *FakeWriterChannel
removedWriters []messaging.Writer
}
func newFakeWriterController() *FakeWriterController {
return &FakeWriterController{channel: newFakeWriterChannel()}
}
func (this *FakeWriterController) openChannel(callback func() bool) Channel {
if !callback() {
return nil
}
if this.channel == nil {
return nil // interface quirks require this hack
}
return this.channel
}
func (this *FakeWriterController) removeReader(reader messaging.Reader) {}
func (this *FakeWriterController) removeWriter(writer messaging.Writer) {
this.removedWriters = append(this.removedWriters, writer)
}
func (this *FakeWriterController) Dispose() {
this.channel = nil
}
///////////////////////////////////////////////////////////////
type FakeWriterChannel struct {
closed int
commits int
writes int
exchange string
partition string
dispatch amqp.Publishing
transactional bool
err error
}
func newFakeWriterChannel() *FakeWriterChannel {
return &FakeWriterChannel{}
}
func (this *FakeWriterChannel) ConfigureChannelBuffer(int) error { return nil }
func (this *FakeWriterChannel) DeclareExchange(string, string) error { return nil }
func (this *FakeWriterChannel) DeclareQueue(string) error { return nil }
func (this *FakeWriterChannel) DeclareTransientQueue() (string, error) { return "", nil }
func (this *FakeWriterChannel) BindExchangeToQueue(string, string) error { return nil }
func (this *FakeWriterChannel) Consume(string, string) (<-chan amqp.Delivery, error) { return nil, nil }
func (this *FakeWriterChannel) ExclusiveConsume(string, string) (<-chan amqp.Delivery, error) {
return nil, nil
}
func (this *FakeWriterChannel) ConsumeWithoutAcknowledgement(string, string) (<-chan amqp.Delivery, error) {
return nil, nil
}
func (this *FakeWriterChannel) ExclusiveConsumeWithoutAcknowledgement(string, string) (<-chan amqp.Delivery, error) {
return nil, nil
}
func (this *FakeWriterChannel) CancelConsumer(string) error { return nil }
func (this *FakeWriterChannel) Close() error {
this.closed++
return nil
}
func (this *FakeWriterChannel) AcknowledgeSingleMessage(uint64) error { return nil }
func (this *FakeWriterChannel) AcknowledgeMultipleMessages(value uint64) error { return nil }
func (this *FakeWriterChannel) ConfigureChannelAsTransactional() error {
this.transactional = true
return nil
}
func (this *FakeWriterChannel) PublishMessage(destination, partition string, dispatch amqp.Publishing) error {
this.exchange = destination
this.partition = partition
this.dispatch = dispatch
this.writes++
return this.err
}
func (this *FakeWriterChannel) CommitTransaction() error {
this.commits++
return this.err
}
func (this *FakeWriterChannel) RollbackTransaction() error { return nil }