Skip to content

Commit

Permalink
blacklist test
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Jan 15, 2019
1 parent 654b4e9 commit 1a05f13
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions blacklist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package pubsub

import (
"context"
"testing"
"time"
)

func TestBlacklist(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

hosts := getNetHosts(t, ctx, 2)
psubs := getPubsubs(ctx, hosts)
connect(t, hosts[0], hosts[1])

sub, err := psubs[1].Subscribe("test")
if err != nil {
t.Fatal(err)
}

time.Sleep(time.Millisecond * 100)
psubs[1].BlacklistPeer(hosts[0].ID())
time.Sleep(time.Millisecond * 100)

psubs[0].Publish("test", []byte("message"))

wctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
_, err = sub.Next(wctx)

if err == nil {
t.Fatal("got message from blacklisted peer")
}
}

func TestBlacklist2(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

hosts := getNetHosts(t, ctx, 2)
psubs := getPubsubs(ctx, hosts)
connect(t, hosts[0], hosts[1])

_, err := psubs[0].Subscribe("test")
if err != nil {
t.Fatal(err)
}

sub1, err := psubs[1].Subscribe("test")
if err != nil {
t.Fatal(err)
}

time.Sleep(time.Millisecond * 100)
psubs[1].BlacklistPeer(hosts[0].ID())
time.Sleep(time.Millisecond * 100)

psubs[0].Publish("test", []byte("message"))

wctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
_, err = sub1.Next(wctx)

if err == nil {
t.Fatal("got message from blacklisted peer")
}
}

func TestBlacklist3(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

hosts := getNetHosts(t, ctx, 2)
psubs := getPubsubs(ctx, hosts)

psubs[1].BlacklistPeer(hosts[0].ID())
time.Sleep(time.Millisecond * 100)
connect(t, hosts[0], hosts[1])

sub, err := psubs[1].Subscribe("test")
if err != nil {
t.Fatal(err)
}

time.Sleep(time.Millisecond * 100)

psubs[0].Publish("test", []byte("message"))

wctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
_, err = sub.Next(wctx)

if err == nil {
t.Fatal("got message from blacklisted peer")
}
}

0 comments on commit 1a05f13

Please sign in to comment.