A correction to the previous commit.
[stompngo.git] / trans_test.go
blobda739b3f1771a6bb7442b7a9f197c1f26ffe4524
1 //
2 // Copyright © 2011-2019 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 "log"
21 "testing"
25 Test transaction errors.
27 func TestTransErrors(t *testing.T) {
28 for pi, sp := range Protocols() {
29 n, _ = openConn(t)
30 ch := login_headers
31 ch = headersProtocol(ch, sp)
32 conn, e = Connect(n, ch)
33 if e != nil {
34 t.Fatalf("TestTransErrors[%d/%s] CONNECT expected OK, got: %v\n", pi,
35 sp, e)
37 for ti, tv := range transBasicList {
38 switch tv.action {
39 case BEGIN:
40 e = conn.Begin(tv.th)
41 case COMMIT:
42 e = conn.Commit(tv.th)
43 case ABORT:
44 e = conn.Abort(tv.th)
45 default:
46 t.Fatalf("TestTransErrors[%d/%s] %s BAD DATA[%d]\n", pi,
47 sp, tv.action, ti)
49 if e == nil {
50 t.Fatalf("TestTransErrors[%d/%s] %s expected error[%d], got %v\n", pi,
51 sp, tv.action, ti, e)
53 if e != tv.te {
54 t.Fatalf("TestTransErrors[%d/%s] %s expected[%d]: %v, got %v\n", pi,
55 sp, tv.action, ti, tv.te, e)
58 checkReceived(t, conn, false)
59 e = conn.Disconnect(empty_headers)
60 checkDisconnectError(t, e)
61 _ = closeConn(t, n)
66 Test transaction send and commit.
68 func TestTransSendCommit(t *testing.T) {
70 for pi, sp := range Protocols() {
71 n, _ = openConn(t)
72 ch := login_headers
73 ch = headersProtocol(ch, sp)
74 conn, _ = Connect(n, ch)
75 if e != nil {
76 t.Fatalf("TestTransSendCommit[%d/%s] CONNECT expected OK, got: %v\n",
77 pi,
78 sp, e)
81 for ti, tv := range transSendCommitList {
82 // BEGIN
83 e = conn.Begin(Headers{HK_TRANSACTION, tv.tid})
84 if e != nil {
85 t.Fatalf("TestTransSendCommit BEGIN[%d][%d] expected [%v], got: [%v]\n",
86 pi, ti, tv.exe, e)
88 // SEND
89 qn := tdest("/queue/" + tv.tid + ".1")
90 log.Println("TSCQN:", qn)
91 sh := Headers{HK_DESTINATION, qn,
92 HK_TRANSACTION, tv.tid}
93 e = conn.Send(sh, tm)
94 if e != nil {
95 t.Fatalf("TestTransSendCommit SEND[%d][%d] expected [%v], got: [%v]\n",
96 pi, ti, tv.exe, e)
98 // COMMIT
99 e = conn.Commit(Headers{HK_TRANSACTION, tv.tid})
100 if e != nil {
101 t.Fatalf("TestTransSendCommit COMMIT[%d][%d] expected [%v], got: [%v]\n",
102 pi, ti, tv.exe, e)
106 checkReceived(t, conn, false)
107 e = conn.Disconnect(empty_headers)
108 checkDisconnectError(t, e)
109 _ = closeConn(t, n)
114 Test transaction send then abort.
117 func TestTransSendAbort(t *testing.T) {
119 for pi, sp := range Protocols() {
120 n, _ = openConn(t)
121 ch := login_headers
122 ch = headersProtocol(ch, sp)
123 conn, _ = Connect(n, ch)
124 if e != nil {
125 t.Fatalf("TestTransSendAbort[%d/%s] CONNECT expected OK, got: %v\n",
127 sp, e)
129 for ti, tv := range transSendAbortList {
130 // BEGIN
131 e = conn.Begin(Headers{HK_TRANSACTION, tv.tid})
132 if e != nil {
133 t.Fatalf("TestTransSendAbort BEGIN[%d][%d] expected [%v], got: [%v]\n",
134 pi, ti, tv.exe, e)
136 // SEND
137 sh := Headers{HK_DESTINATION, tdest("/queue/" + tv.tid + ".1"),
138 HK_TRANSACTION, tv.tid}
139 e = conn.Send(sh, tm)
140 if e != nil {
141 t.Fatalf("TestTransSendAbort SEND[%d][%d] expected [%v], got: [%v]\n",
142 pi, ti, tv.exe, e)
144 // ABORT
145 e = conn.Abort(Headers{HK_TRANSACTION, tv.tid})
146 if e != nil {
147 t.Fatalf("TestTransSendAbort COMMIT[%d][%d] expected [%v], got: [%v]\n",
148 pi, ti, tv.exe, e)
152 checkReceived(t, conn, false)
153 e = conn.Disconnect(empty_headers)
154 checkDisconnectError(t, e)
155 _ = closeConn(t, n)