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
/
transaction_writer_test.go
103 lines (75 loc) · 2.98 KB
/
transaction_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
package rabbitmq
import (
"errors"
"testing"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
"github.com/smartystreets/messaging/v2"
)
func TestTransactionWriterFixture(t *testing.T) {
gunit.Run(new(TransactionWriterFixture), t)
}
type TransactionWriterFixture struct {
*gunit.Fixture
writer *TransactionWriter
controller *FakeWriterController
}
func (this *TransactionWriterFixture) Setup() {
this.controller = newFakeWriterController()
this.writer = transactionWriter(this.controller)
}
///////////////////////////////////////////////////////////////
func (this *TransactionWriterFixture) 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.BeTrue)
}
///////////////////////////////////////////////////////////////
func (this *TransactionWriterFixture) TestChannelCannotBeObtained() {
this.controller.channel = nil
err := this.writer.Write(messaging.Dispatch{})
this.So(err, should.NotBeNil)
}
///////////////////////////////////////////////////////////////
func (this *TransactionWriterFixture) TestFailedChannelNOTClosedOnFailedWrites() {
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, 0)
this.So(this.writer.channel, should.NotBeNil)
}
///////////////////////////////////////////////////////////////
func (this *TransactionWriterFixture) TestCloseWriter() {
this.writer.Close()
this.So(this.writer.closed, should.BeTrue)
this.So(this.writer.Write(messaging.Dispatch{}), should.Equal, messaging.ErrWriterClosed)
}
///////////////////////////////////////////////////////////////
func (this *TransactionWriterFixture) TestCommitWithoutIsNoop() {
err := this.writer.Commit()
this.So(err, should.BeNil)
this.So(this.controller.channel.commits, should.Equal, 0)
}
func (this *TransactionWriterFixture) TestCommitCallsUnderlyingChannel() {
_ = this.writer.Write(messaging.Dispatch{})
err := this.writer.Commit()
this.So(err, should.BeNil)
this.So(this.controller.channel.commits, should.Equal, 1)
}
func (this *TransactionWriterFixture) TestFailedCommitsReturnError() {
_ = this.writer.Write(messaging.Dispatch{})
this.controller.channel.err = errors.New("Commit failure")
err := this.writer.Commit()
this.So(err, should.Equal, this.controller.channel.err)
this.So(this.controller.channel.commits, should.Equal, 1)
this.So(this.controller.channel.closed, should.Equal, 1)
this.So(this.writer.channel, should.BeNil)
}