|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package tcp |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + iggcon "github.com/apache/iggy/foreign/go/contracts" |
| 25 | +) |
| 26 | + |
| 27 | +func recvWithTimeout(t *testing.T, ch <-chan iggcon.DiagnosticEvent) (iggcon.DiagnosticEvent, bool) { |
| 28 | + t.Helper() |
| 29 | + select { |
| 30 | + case ev, ok := <-ch: |
| 31 | + return ev, ok |
| 32 | + case <-time.After(time.Second): |
| 33 | + t.Fatal("timed out waiting for event") |
| 34 | + return 0, false |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestEventBroadcaster_DeliversToAllSubscribers(t *testing.T) { |
| 39 | + b := newEventBroadcaster() |
| 40 | + a := b.subscribe() |
| 41 | + c := b.subscribe() |
| 42 | + |
| 43 | + b.publish(iggcon.DiagnosticEventConnected) |
| 44 | + |
| 45 | + if ev, _ := recvWithTimeout(t, a); ev != iggcon.DiagnosticEventConnected { |
| 46 | + t.Errorf("subscriber A got %v, want connected", ev) |
| 47 | + } |
| 48 | + if ev, _ := recvWithTimeout(t, c); ev != iggcon.DiagnosticEventConnected { |
| 49 | + t.Errorf("subscriber B got %v, want connected", ev) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestEventBroadcaster_PreservesOrderForOneSubscriber(t *testing.T) { |
| 54 | + b := newEventBroadcaster() |
| 55 | + ch := b.subscribe() |
| 56 | + |
| 57 | + want := []iggcon.DiagnosticEvent{ |
| 58 | + iggcon.DiagnosticEventConnected, |
| 59 | + iggcon.DiagnosticEventSignedIn, |
| 60 | + iggcon.DiagnosticEventDisconnected, |
| 61 | + } |
| 62 | + for _, ev := range want { |
| 63 | + b.publish(ev) |
| 64 | + } |
| 65 | + for i, w := range want { |
| 66 | + got, _ := recvWithTimeout(t, ch) |
| 67 | + if got != w { |
| 68 | + t.Errorf("event %d: got %v, want %v", i, got, w) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestEventBroadcaster_CloseDeliversNoMoreEvents(t *testing.T) { |
| 74 | + b := newEventBroadcaster() |
| 75 | + ch := b.subscribe() |
| 76 | + |
| 77 | + b.close() |
| 78 | + |
| 79 | + // channel should be closed; receive returns zero, ok=false |
| 80 | + select { |
| 81 | + case _, ok := <-ch: |
| 82 | + if ok { |
| 83 | + t.Fatal("expected channel to be closed") |
| 84 | + } |
| 85 | + case <-time.After(time.Second): |
| 86 | + t.Fatal("timed out waiting for closed channel") |
| 87 | + } |
| 88 | + |
| 89 | + // subsequent publish is a no-op |
| 90 | + b.publish(iggcon.DiagnosticEventConnected) |
| 91 | + |
| 92 | + // late subscribe returns an already-closed channel |
| 93 | + late := b.subscribe() |
| 94 | + select { |
| 95 | + case _, ok := <-late: |
| 96 | + if ok { |
| 97 | + t.Fatal("expected late-subscribe channel to be closed") |
| 98 | + } |
| 99 | + case <-time.After(time.Second): |
| 100 | + t.Fatal("timed out waiting for closed late-subscribe channel") |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestEventBroadcaster_SlowSubscriberDoesNotBlockPublisher(t *testing.T) { |
| 105 | + b := newEventBroadcaster() |
| 106 | + _ = b.subscribe() // never drained |
| 107 | + |
| 108 | + // Publish more events than the buffer; if the publisher blocked or |
| 109 | + // panicked we'd hang or fail here. |
| 110 | + for range subscriberBufferSize * 2 { |
| 111 | + b.publish(iggcon.DiagnosticEventConnected) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestEventBroadcaster_CloseIsIdempotent(t *testing.T) { |
| 116 | + b := newEventBroadcaster() |
| 117 | + b.subscribe() |
| 118 | + b.close() |
| 119 | + b.close() // must not panic |
| 120 | +} |
0 commit comments