Rework writer deadline handling.
[stompngo.git] / trans_test.go
bloba3abfd7321067a3e72cf7b95ca0dbb424352ad35
1 //
2 // Copyright © 2011-2017 Guy M. Allard
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 package stompngo
19 import (
20 "testing"
24 Test transaction errors.
26 func TestTransErrors(t *testing.T) {
27 for pi, sp := range Protocols() {
28 n, _ = openConn(t)
29 ch := login_headers
30 ch = headersProtocol(ch, sp)
31 conn, e = Connect(n, ch)
32 if e != nil {
33 t.Fatalf("TestTransErrors[%d/%s] CONNECT expected OK, got: %v\n", pi,
34 sp, e)
36 for ti, tv := range transBasicList {
37 switch tv.action {
38 case BEGIN:
39 e = conn.Begin(tv.th)
40 case COMMIT:
41 e = conn.Commit(tv.th)
42 case ABORT:
43 e = conn.Abort(tv.th)
44 default:
45 t.Fatalf("TestTransErrors[%d/%s] %s BAD DATA[%d]\n", pi,
46 sp, tv.action, ti)
48 if e == nil {
49 t.Fatalf("TestTransErrors[%d/%s] %s expected error[%d], got %v\n", pi,
50 sp, tv.action, ti, e)
52 if e != tv.te {
53 t.Fatalf("TestTransErrors[%d/%s] %s expected[%d]: %v, got %v\n", pi,
54 sp, tv.action, ti, tv.te, e)
57 checkReceived(t, conn)
58 e = conn.Disconnect(empty_headers)
59 checkDisconnectError(t, e)
60 _ = closeConn(t, n)
65 Test transaction send and commit.
67 func TestTransSendCommit(t *testing.T) {
69 for pi, sp := range Protocols() {
70 n, _ = openConn(t)
71 ch := login_headers
72 ch = headersProtocol(ch, sp)
73 conn, _ = Connect(n, ch)
74 if e != nil {
75 t.Fatalf("TestTransSendCommit[%d/%s] CONNECT expected OK, got: %v\n",
76 pi,
77 sp, e)
80 for ti, tv := range transSendCommitList {
81 // BEGIN
82 e = conn.Begin(Headers{HK_TRANSACTION, tv.tid})
83 if e != nil {
84 t.Fatalf("TestTransSendCommit BEGIN[%d][%d] expected [%v], got: [%v]\n",
85 pi, ti, tv.exe, e)
87 // SEND
88 sh := Headers{HK_DESTINATION, tdest("/queue/" + tv.tid + ".1"),
89 HK_TRANSACTION, tv.tid}
90 e = conn.Send(sh, tm)
91 if e != nil {
92 t.Fatalf("TestTransSendCommit SEND[%d][%d] expected [%v], got: [%v]\n",
93 pi, ti, tv.exe, e)
95 // COMMIT
96 e = conn.Commit(Headers{HK_TRANSACTION, tv.tid})
97 if e != nil {
98 t.Fatalf("TestTransSendCommit COMMIT[%d][%d] expected [%v], got: [%v]\n",
99 pi, ti, tv.exe, e)
103 checkReceived(t, conn)
104 e = conn.Disconnect(empty_headers)
105 checkDisconnectError(t, e)
106 _ = closeConn(t, n)
111 Test transaction send then abort.
114 func TestTransSendAbort(t *testing.T) {
116 for pi, sp := range Protocols() {
117 n, _ = openConn(t)
118 ch := login_headers
119 ch = headersProtocol(ch, sp)
120 conn, _ = Connect(n, ch)
121 if e != nil {
122 t.Fatalf("TestTransSendAbort[%d/%s] CONNECT expected OK, got: %v\n",
124 sp, e)
126 for ti, tv := range transSendAbortList {
127 // BEGIN
128 e = conn.Begin(Headers{HK_TRANSACTION, tv.tid})
129 if e != nil {
130 t.Fatalf("TestTransSendAbort BEGIN[%d][%d] expected [%v], got: [%v]\n",
131 pi, ti, tv.exe, e)
133 // SEND
134 sh := Headers{HK_DESTINATION, tdest("/queue/" + tv.tid + ".1"),
135 HK_TRANSACTION, tv.tid}
136 e = conn.Send(sh, tm)
137 if e != nil {
138 t.Fatalf("TestTransSendAbort SEND[%d][%d] expected [%v], got: [%v]\n",
139 pi, ti, tv.exe, e)
141 // ABORT
142 e = conn.Abort(Headers{HK_TRANSACTION, tv.tid})
143 if e != nil {
144 t.Fatalf("TestTransSendAbort COMMIT[%d][%d] expected [%v], got: [%v]\n",
145 pi, ti, tv.exe, e)
149 checkReceived(t, conn)
150 e = conn.Disconnect(empty_headers)
151 checkDisconnectError(t, e)
152 _ = closeConn(t, n)