aboutsummaryrefslogtreecommitdiffstats
path: root/event/feed_test.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@users.noreply.github.com>2018-05-10 18:26:36 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-05-10 18:26:36 +0800
commit53a18d2e2734d078200ec607055ae551245ae74b (patch)
tree0281d5d7a186cc6d79c237091b607d35a510746a /event/feed_test.go
parent7beccb29becf439df7bf4c033a94c019ad25bead (diff)
downloaddexon-53a18d2e2734d078200ec607055ae551245ae74b.tar
dexon-53a18d2e2734d078200ec607055ae551245ae74b.tar.gz
dexon-53a18d2e2734d078200ec607055ae551245ae74b.tar.bz2
dexon-53a18d2e2734d078200ec607055ae551245ae74b.tar.lz
dexon-53a18d2e2734d078200ec607055ae551245ae74b.tar.xz
dexon-53a18d2e2734d078200ec607055ae551245ae74b.tar.zst
dexon-53a18d2e2734d078200ec607055ae551245ae74b.zip
event: document select case slice use and add edge case test (#16680)
Feed keeps active subscription channels in a slice called 'f.sendCases'. The Send method tracks the active cases in a local variable 'cases' whose value is f.sendCases initially. 'cases' shrinks to a shorter prefix of f.sendCases every time a send succeeds, moving the successful case out of range of the active case list. This can be confusing because the two slices share a backing array. Add more comments to document what is going on. Also add a test for removing a case that is in 'f.sentCases' but not 'cases'.
Diffstat (limited to 'event/feed_test.go')
-rw-r--r--event/feed_test.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/event/feed_test.go b/event/feed_test.go
index a82c10303..be8876932 100644
--- a/event/feed_test.go
+++ b/event/feed_test.go
@@ -235,6 +235,45 @@ func TestFeedUnsubscribeBlockedPost(t *testing.T) {
wg.Wait()
}
+// Checks that unsubscribing a channel during Send works even if that
+// channel has already been sent on.
+func TestFeedUnsubscribeSentChan(t *testing.T) {
+ var (
+ feed Feed
+ ch1 = make(chan int)
+ ch2 = make(chan int)
+ sub1 = feed.Subscribe(ch1)
+ sub2 = feed.Subscribe(ch2)
+ wg sync.WaitGroup
+ )
+ defer sub2.Unsubscribe()
+
+ wg.Add(1)
+ go func() {
+ feed.Send(0)
+ wg.Done()
+ }()
+
+ // Wait for the value on ch1.
+ <-ch1
+ // Unsubscribe ch1, removing it from the send cases.
+ sub1.Unsubscribe()
+
+ // Receive ch2, finishing Send.
+ <-ch2
+ wg.Wait()
+
+ // Send again. This should send to ch2 only, so the wait group will unblock
+ // as soon as a value is received on ch2.
+ wg.Add(1)
+ go func() {
+ feed.Send(0)
+ wg.Done()
+ }()
+ <-ch2
+ wg.Wait()
+}
+
func TestFeedUnsubscribeFromInbox(t *testing.T) {
var (
feed Feed