notifications_test.go 4.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
package notifications_test

import (
	"context"
	"testing"
	"time"

	"github.com/ipfs/go-graphsync/notifications"
	"github.com/ipfs/go-graphsync/testutil"
)

func TestSubscribeOn(t *testing.T) {
	ctx := context.Background()
	testCases := map[string]func(ctx context.Context, t *testing.T, ps notifications.Publisher){
		"SubscribeOn": func(ctx context.Context, t *testing.T, ps notifications.Publisher) {
			destTopic := "t2"
			notifee, verifier := testutil.NewTestNotifee(destTopic, 1)
			notifications.SubscribeOn(ps, "t1", notifee)
			ps.Publish("t1", "hi")
			ps.Shutdown()
			verifier.ExpectEvents(ctx, t, []notifications.Event{"hi"})
			verifier.ExpectClose(ctx, t)
		},
		"Add subscriptions": func(ctx context.Context, t *testing.T, ps notifications.Publisher) {
			sub1 := testutil.NewTestSubscriber(3)
			sub2 := testutil.NewTestSubscriber(3)
			ps.Subscribe("t1", sub1)
			ps.Subscribe("t2", sub2)

			ps.Publish("t1", "hi1")
			ps.Publish("t2", "hi2")

			ps.Subscribe("t2", sub1)
			ps.Subscribe("t3", sub1)

			ps.Publish("t2", "hi3")
			ps.Publish("t3", "hi4")

			ps.Shutdown()
			sub1.ExpectEvents(ctx, t, []testutil.DispatchedEvent{
				{
					Topic: "t1",
					Event: "hi1",
				},
				{
					Topic: "t2",
					Event: "hi3",
				},
				{
					Topic: "t3",
					Event: "hi4",
				},
			})
			sub1.ExpectClosesAnyOrder(ctx, t, []notifications.Topic{"t1", "t2", "t3"})
			sub2.ExpectEvents(ctx, t, []testutil.DispatchedEvent{
				{Topic: "t2", Event: "hi2"}, {Topic: "t2", Event: "hi3"},
			})
			sub2.ExpectClosesAnyOrder(ctx, t, []notifications.Topic{"t2"})
		},
		"Unsubscribe": func(ctx context.Context, t *testing.T, ps notifications.Publisher) {
			sub1 := testutil.NewTestSubscriber(3)
			sub2 := testutil.NewTestSubscriber(3)
			ps.Subscribe("t1", sub1)
			ps.Subscribe("t2", sub1)
			ps.Subscribe("t3", sub1)
			ps.Subscribe("t1", sub2)
			ps.Subscribe("t3", sub2)
			ps.Unsubscribe(sub1)
			ps.Publish("t1", "hi")
			ps.Shutdown()

			sub2.ExpectEvents(ctx, t, []testutil.DispatchedEvent{
				{Topic: "t1", Event: "hi"},
			})
			sub2.ExpectClosesAnyOrder(ctx, t, []notifications.Topic{"t1", "t3"})
			sub1.ExpectClosesAnyOrder(ctx, t, []notifications.Topic{"t1", "t2", "t3"})
			sub1.NoEventsReceived(t)
		},
		"Close": func(ctx context.Context, t *testing.T, ps notifications.Publisher) {
			sub1 := testutil.NewTestSubscriber(3)
			sub2 := testutil.NewTestSubscriber(3)
			sub3 := testutil.NewTestSubscriber(3)
			sub4 := testutil.NewTestSubscriber(3)
			ps.Subscribe("t1", sub1)
			ps.Subscribe("t1", sub2)
			ps.Subscribe("t2", sub3)
			ps.Subscribe("t3", sub4)

			ps.Publish("t1", "hi")
			ps.Publish("t2", "hello")
			ps.Close("t1")
			ps.Close("t2")

			sub1.ExpectEvents(ctx, t, []testutil.DispatchedEvent{
				{Topic: "t1", Event: "hi"},
			})
			sub1.ExpectClosesAnyOrder(ctx, t, []notifications.Topic{"t1"})
			sub2.ExpectEvents(ctx, t, []testutil.DispatchedEvent{
				{Topic: "t1", Event: "hi"},
			})
			sub2.ExpectClosesAnyOrder(ctx, t, []notifications.Topic{"t1"})
			sub3.ExpectEvents(ctx, t, []testutil.DispatchedEvent{
				{Topic: "t2", Event: "hello"},
			})
			sub3.ExpectClosesAnyOrder(ctx, t, []notifications.Topic{"t2"})

			// publishing on a topic after close should be like starting from scratch
			// -- no one listening before should receive events
			ps.Publish("t1", "hi")
			ps.Publish("t2", "hi")

			ps.Publish("t3", "welcome")
			ps.Shutdown()

			sub4.ExpectEvents(ctx, t, []testutil.DispatchedEvent{
				{Topic: "t3", Event: "welcome"},
			})
			sub4.ExpectClosesAnyOrder(ctx, t, []notifications.Topic{"t3"})
			sub1.NoEventsReceived(t)
			sub2.NoEventsReceived(t)
			sub3.NoEventsReceived(t)
		},
		"Shutdown": func(ctx context.Context, t *testing.T, ps notifications.Publisher) {
			sub1 := testutil.NewTestSubscriber(3)
			sub2 := testutil.NewTestSubscriber(3)
			ps.Subscribe("t1", sub1)
			ps.Subscribe("t2", sub2)

			ps.Shutdown()

			// operations after shutdown have no effect
			ps.Publish("t1", "hi")
			ps.Publish("t2", "hello")
			sub1.ExpectClosesAnyOrder(ctx, t, []notifications.Topic{"t1"})
			sub2.ExpectClosesAnyOrder(ctx, t, []notifications.Topic{"t2"})
			time.Sleep(100 * time.Millisecond)
			sub1.NoEventsReceived(t)
			sub2.NoEventsReceived(t)
		},
	}
	for testCase, testPublisher := range testCases {
		t.Run(testCase, func(t *testing.T) {
			ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
			defer cancel()
			ps := notifications.NewPublisher()
146
			ps.Startup()
147
			testPublisher(ctx, t, ps)
148
			ps.Shutdown()
149 150 151 152
		})
	}

}