github.com/turingchain2020/turingchain@v1.1.21/common/pubsub/pubsub_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Copyright 2013, Chandra Sekar S. All rights reserved. 6 // Use of this source code is governed by a BSD-style 7 // license that can be found in the README.md file. 8 package pubsub 9 10 import ( 11 "testing" 12 "time" 13 14 check "gopkg.in/check.v1" 15 ) 16 17 var _ = check.Suite(new(Suite)) 18 19 func Test(t *testing.T) { 20 check.TestingT(t) 21 } 22 23 type Suite struct{} 24 25 func (s *Suite) TestSub(c *check.C) { 26 ps := NewPubSub(1) 27 ch1 := ps.Sub("t1") 28 ch2 := ps.Sub("t1") 29 ch3 := ps.Sub("t2") 30 31 ps.Pub("hi", "t1") 32 c.Check(<-ch1, check.Equals, "hi") 33 c.Check(<-ch2, check.Equals, "hi") 34 35 ps.Pub("hello", "t2") 36 c.Check(<-ch3, check.Equals, "hello") 37 38 ps.Shutdown() 39 _, ok := <-ch1 40 c.Check(ok, check.Equals, false) 41 _, ok = <-ch2 42 c.Check(ok, check.Equals, false) 43 _, ok = <-ch3 44 c.Check(ok, check.Equals, false) 45 } 46 47 func (s *Suite) TestSubOnce(c *check.C) { 48 ps := NewPubSub(1) 49 ch := ps.SubOnce("t1") 50 51 ps.Pub("hi", "t1") 52 c.Check(<-ch, check.Equals, "hi") 53 54 _, ok := <-ch 55 c.Check(ok, check.Equals, false) 56 ps.Shutdown() 57 } 58 59 func (s *Suite) TestAddSub(c *check.C) { 60 ps := NewPubSub(1) 61 ch1 := ps.Sub("t1") 62 ch2 := ps.Sub("t2") 63 64 ps.Pub("hi1", "t1") 65 c.Check(<-ch1, check.Equals, "hi1") 66 67 ps.Pub("hi2", "t2") 68 c.Check(<-ch2, check.Equals, "hi2") 69 70 ps.AddSub(ch1, "t2", "t3") 71 ps.Pub("hi3", "t2") 72 c.Check(<-ch1, check.Equals, "hi3") 73 c.Check(<-ch2, check.Equals, "hi3") 74 75 ps.Pub("hi4", "t3") 76 c.Check(<-ch1, check.Equals, "hi4") 77 78 ps.Shutdown() 79 } 80 81 func (s *Suite) TestUnsub(c *check.C) { 82 ps := NewPubSub(1) 83 ch := ps.Sub("t1") 84 85 ps.Pub("hi", "t1") 86 c.Check(<-ch, check.Equals, "hi") 87 88 ps.Unsub(ch, "t1") 89 _, ok := <-ch 90 c.Check(ok, check.Equals, false) 91 ps.Shutdown() 92 } 93 94 func (s *Suite) TestUnsubAll(c *check.C) { 95 ps := NewPubSub(1) 96 ch1 := ps.Sub("t1", "t2", "t3") 97 ch2 := ps.Sub("t1", "t3") 98 99 ps.Unsub(ch1) 100 101 _, ok := <-ch1 102 c.Check(ok, check.Equals, false) 103 104 ps.Pub("hi", "t1") 105 m := <-ch2 106 c.Check(m, check.Equals, "hi") 107 108 ps.Shutdown() 109 } 110 111 func (s *Suite) TestClose(c *check.C) { 112 ps := NewPubSub(1) 113 ch1 := ps.Sub("t1") 114 ch2 := ps.Sub("t1") 115 ch3 := ps.Sub("t2") 116 ch4 := ps.Sub("t3") 117 118 ps.Pub("hi", "t1") 119 ps.Pub("hello", "t2") 120 c.Check(<-ch1, check.Equals, "hi") 121 c.Check(<-ch2, check.Equals, "hi") 122 c.Check(<-ch3, check.Equals, "hello") 123 124 ps.Close("t1", "t2") 125 _, ok := <-ch1 126 c.Check(ok, check.Equals, false) 127 _, ok = <-ch2 128 c.Check(ok, check.Equals, false) 129 _, ok = <-ch3 130 c.Check(ok, check.Equals, false) 131 132 ps.Pub("welcome", "t3") 133 c.Check(<-ch4, check.Equals, "welcome") 134 135 ps.Shutdown() 136 } 137 138 func (s *Suite) TestUnsubAfterClose(c *check.C) { 139 ps := NewPubSub(1) 140 ch := ps.Sub("t1") 141 defer func() { 142 ps.Unsub(ch, "t1") 143 ps.Shutdown() 144 }() 145 146 ps.Close("t1") 147 _, ok := <-ch 148 c.Check(ok, check.Equals, false) 149 } 150 151 /* 152 func (s *Suite) TestShutdown(c *check.C) { 153 start := runtime.NumGoroutine() 154 pprof.Lookup("goroutine").WriteTo(os.Stdout, 2) 155 NewPubSub(10).Shutdown() 156 time.Sleep(2000) 157 println("==================================") 158 pprof.Lookup("goroutine").WriteTo(os.Stdout, 2) 159 c.Check(runtime.NumGoroutine(), check.Equals, start) 160 } 161 */ 162 163 func (s *Suite) TestMultiSub(c *check.C) { 164 ps := NewPubSub(1) 165 ch := ps.Sub("t1", "t2") 166 167 ps.Pub("hi", "t1") 168 c.Check(<-ch, check.Equals, "hi") 169 170 ps.Pub("hello", "t2") 171 c.Check(<-ch, check.Equals, "hello") 172 173 ps.Shutdown() 174 _, ok := <-ch 175 c.Check(ok, check.Equals, false) 176 } 177 178 func (s *Suite) TestMultiSubOnce(c *check.C) { 179 ps := NewPubSub(1) 180 ch := ps.SubOnce("t1", "t2") 181 182 ps.Pub("hi", "t1") 183 c.Check(<-ch, check.Equals, "hi") 184 185 ps.Pub("hello", "t2") 186 187 _, ok := <-ch 188 c.Check(ok, check.Equals, false) 189 ps.Shutdown() 190 } 191 192 func (s *Suite) TestMultiPub(c *check.C) { 193 ps := NewPubSub(1) 194 ch1 := ps.Sub("t1") 195 ch2 := ps.Sub("t2") 196 197 ps.Pub("hi", "t1", "t2") 198 c.Check(<-ch1, check.Equals, "hi") 199 c.Check(<-ch2, check.Equals, "hi") 200 201 ps.Shutdown() 202 } 203 204 func (s *Suite) TestTryPub(c *check.C) { 205 ps := NewPubSub(1) 206 ch := ps.Sub("t1") 207 208 ps.TryPub("hi", "t1") 209 ps.TryPub("there", "t1") 210 211 <-ch 212 extraMsg := false 213 select { 214 case <-ch: 215 extraMsg = true 216 default: 217 } 218 c.Check(extraMsg, check.Equals, false) 219 220 ps.Shutdown() 221 } 222 223 func (s *Suite) TestFIFOPub(c *check.C) { 224 ps := NewPubSub(1) //订阅的buffer 为 1,也就是发两个数据就满了 225 226 ch1 := ps.Sub("t1") 227 ch2 := ps.Sub("t1") 228 ps.FIFOPub("hi", "t1") 229 ps.FIFOPub("there", "t1") 230 time.Sleep(time.Second) 231 c.Check(<-ch1, check.Equals, "there") 232 c.Check(<-ch2, check.Equals, "there") 233 ps.Shutdown() 234 } 235 236 func (s *Suite) TestMultiUnsub(c *check.C) { 237 ps := NewPubSub(1) 238 ch := ps.Sub("t1", "t2", "t3") 239 240 ps.Unsub(ch, "t1") 241 242 ps.Pub("hi", "t1") 243 244 ps.Pub("hello", "t2") 245 c.Check(<-ch, check.Equals, "hello") 246 247 ps.Unsub(ch, "t2", "t3") 248 _, ok := <-ch 249 c.Check(ok, check.Equals, false) 250 251 ps.Shutdown() 252 } 253 254 func (s *Suite) TestMultiClose(c *check.C) { 255 ps := NewPubSub(1) 256 ch := ps.Sub("t1", "t2") 257 258 ps.Pub("hi", "t1") 259 c.Check(<-ch, check.Equals, "hi") 260 261 ps.Close("t1") 262 ps.Pub("hello", "t2") 263 c.Check(<-ch, check.Equals, "hello") 264 265 ps.Close("t2") 266 _, ok := <-ch 267 c.Check(ok, check.Equals, false) 268 269 ps.Shutdown() 270 }