Address issue #50.
[stompngo.git] / unsub_test.go
blobc8fccaecc0702c4bae655eaf0f47427a564e49b6
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 //"fmt"
21 "log"
23 //"os"
24 "testing"
25 //"time"
28 func TestUnSubNoHeader(t *testing.T) {
29 n, _ = openConn(t)
30 ch := login_headers
31 ch = headersProtocol(ch, SPL_10) // To start
32 conn, e = Connect(n, ch)
33 if e != nil {
34 t.Fatalf("TestUnSubNoHeader CONNECT Failed: e:<%q> connresponse:<%q>\n", e,
35 conn.ConnectResponse)
38 for ti, tv := range unsubNoHeaderDataList {
39 conn.protocol = tv.proto // Cheat, fake all protocols
40 e = conn.Unsubscribe(empty_headers)
41 if e == nil {
42 t.Fatalf("TestUnSubNoHeader[%d] proto:%s expected:%q got:nil\n",
43 ti, sp, tv.exe)
45 if e != tv.exe {
46 t.Fatalf("TestUnSubNoHeader[%d] proto:%s expected:%q got:%q\n",
47 ti, sp, tv.exe, e)
51 checkReceived(t, conn, false)
52 e = conn.Disconnect(empty_headers)
53 checkDisconnectError(t, e)
54 _ = closeConn(t, n)
55 log.Printf("TestUnSubNoHeader %d tests complete.\n", len(subNoHeaderDataList))
59 func TestUnSubNoID(t *testing.T) {
60 n, _ = openConn(t)
61 ch := login_headers
62 ch = headersProtocol(ch, SPL_10) // To start
63 conn, e = Connect(n, ch)
64 if e != nil {
65 t.Fatalf("TestUnSubNoID CONNECT Failed: e:<%q> connresponse:<%q>\n", e,
66 conn.ConnectResponse)
69 for ti, tv := range unsubNoHeaderDataList {
70 conn.protocol = tv.proto // Cheat, fake all protocols
71 e = conn.Unsubscribe(empty_headers)
72 if e != tv.exe {
73 t.Fatalf("TestUnSubNoHeader[%d] proto:%s expected:%q got:%q\n",
74 ti, sp, tv.exe, e)
78 checkReceived(t, conn, false)
79 e = conn.Disconnect(empty_headers)
80 checkDisconnectError(t, e)
81 _ = closeConn(t, n)
82 log.Printf("TestUnSubNoID %d tests complete.\n", len(unsubNoHeaderDataList))
85 func TestUnSubBool(t *testing.T) {
86 n, _ = openConn(t)
87 ch := login_headers
88 ch = headersProtocol(ch, SPL_10) // To start
89 conn, e = Connect(n, ch)
90 if e != nil {
91 t.Fatalf("CONNECT Failed: e:<%q> connresponse:<%q>\n", e,
92 conn.ConnectResponse)
95 for ti, tv := range unsubBoolDataList {
96 conn.protoLock.Lock()
97 conn.protocol = tv.proto // Cheat, fake all protocols
98 conn.protoLock.Unlock()
100 // SUBSCRIBE Phase (depending on test data)
101 if tv.subfirst {
102 // Do a real SUBSCRIBE
103 // sc, e = conn.Subscribe
104 sh := fixHeaderDest(tv.subh) // destination fixed if needed
105 sc, e = conn.Subscribe(sh)
106 if e == nil && sc == nil {
107 t.Fatalf("TestUnSubBool[%d] SUBSCRIBE proto:%s expected OK, got <%v> <%v>\n",
108 ti, conn.protocol, e, sc)
110 if sc == nil {
111 t.Fatalf("TestUnSubBool[%d] SUBSCRIBE, proto:[%s], channel is nil\n",
112 ti, tv.proto)
114 if e != tv.exe1 {
115 t.Fatalf("TestUnSubBool[%d] SUBSCRIBE NEQCHECK proto:%s expected:%v got:%q\n",
116 ti, tv.proto, tv.exe1, e)
120 //fmt.Printf("fs,unsubh: <%v>\n", tv.unsubh)
121 // UNSCRIBE Phase
122 sh := fixHeaderDest(tv.unsubh) // destination fixed if needed
123 e = conn.Unsubscribe(sh)
124 if e != tv.exe2 {
125 t.Fatalf("TestUnSubBool[%d] UNSUBSCRIBE NEQCHECK proto:%s expected:%v got:%q\n",
126 ti, tv.proto, tv.exe2, e)
130 checkReceived(t, conn, true) // true for this test
131 _ = conn.Disconnect(empty_headers)
132 _ = closeConn(t, n)
133 log.Printf("TestUnSubBool %d tests complete.\n", len(unsubBoolDataList))