|
| 1 | +package pubsub |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/libp2p/go-libp2p/p2p/protocol/identify" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNotifyPeerProtocolsUpdated(t *testing.T) { |
| 12 | + ctx, cancel := context.WithCancel(context.Background()) |
| 13 | + defer cancel() |
| 14 | + |
| 15 | + hosts := getDefaultHosts(t, 2) |
| 16 | + |
| 17 | + // Initialize id services. |
| 18 | + { |
| 19 | + ids1, err := identify.NewIDService(hosts[0]) |
| 20 | + if err != nil { |
| 21 | + t.Fatal(err) |
| 22 | + } |
| 23 | + ids1.Start() |
| 24 | + defer ids1.Close() |
| 25 | + |
| 26 | + ids2, err := identify.NewIDService(hosts[1]) |
| 27 | + if err != nil { |
| 28 | + t.Fatal(err) |
| 29 | + } |
| 30 | + ids2.Start() |
| 31 | + defer ids2.Close() |
| 32 | + } |
| 33 | + |
| 34 | + psubs0 := getPubsub(ctx, hosts[0]) |
| 35 | + connect(t, hosts[0], hosts[1]) |
| 36 | + // Delay to make sure that peers are connected. |
| 37 | + <-time.After(time.Millisecond * 100) |
| 38 | + psubs1 := getPubsub(ctx, hosts[1]) |
| 39 | + |
| 40 | + // Pubsub 0 joins topic "test". |
| 41 | + topic0, err := psubs0.Join("test") |
| 42 | + if err != nil { |
| 43 | + t.Fatal(err) |
| 44 | + } |
| 45 | + defer topic0.Close() |
| 46 | + |
| 47 | + sub0, err := topic0.Subscribe() |
| 48 | + if err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + defer sub0.Cancel() |
| 52 | + |
| 53 | + // Pubsub 1 joins topic "test". |
| 54 | + topic1, err := psubs1.Join("test") |
| 55 | + if err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + defer topic1.Close() |
| 59 | + |
| 60 | + sub1, err := topic1.Subscribe() |
| 61 | + if err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + defer sub1.Cancel() |
| 65 | + |
| 66 | + // Delay before checking results (similar to most tests). |
| 67 | + <-time.After(time.Millisecond * 100) |
| 68 | + |
| 69 | + if len(topic0.ListPeers()) == 0 { |
| 70 | + t.Fatalf("topic0 should at least have 1 peer") |
| 71 | + } |
| 72 | + |
| 73 | + if len(topic1.ListPeers()) == 0 { |
| 74 | + t.Fatalf("topic1 should at least have 1 peer") |
| 75 | + } |
| 76 | +} |
0 commit comments