| 1 | package hub |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | const testKind Kind = 1 |
| 6 | const testValue = "foo" |
| 7 | |
| 8 | type testEvent string |
| 9 | |
| 10 | func (e testEvent) Kind() Kind { |
| 11 | return testKind |
| 12 | } |
| 13 | |
| 14 | func TestPubSub(t *testing.T) { |
| 15 | var h Hub |
| 16 | var s string |
| 17 | |
| 18 | h.Subscribe(testKind, func(e Event) { s = string(e.(testEvent)) }) |
| 19 | h.Publish(testEvent(testValue)) |
| 20 | |
| 21 | if s != testValue { |
| 22 | t.Errorf("invalid value: %s", s) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func TestCancel(t *testing.T) { |
| 27 | var h Hub |
| 28 | var called int |
| 29 | var f = func(e Event) { called += 1 } |
| 30 | |
| 31 | _ = h.Subscribe(testKind, f) |
| 32 | cancel := h.Subscribe(testKind, f) |
| 33 | h.Publish(testEvent(testValue)) // 2 calls to f |
| 34 | cancel() |
| 35 | h.Publish(testEvent(testValue)) // 1 call to f |
| 36 | |
| 37 | if called != 3 { |
| 38 | t.Errorf("unexpected call count: %d", called) |
| 39 | } |
| 40 | } |