mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-06 00:38:08 +08:00
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package notifications
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
"time"
|
|
|
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
|
|
|
blocks "github.com/jbenet/go-ipfs/blocks"
|
|
)
|
|
|
|
func TestPublishSubscribe(t *testing.T) {
|
|
blockSent := getBlockOrFail(t, "Greetings from The Interval")
|
|
|
|
n := New()
|
|
defer n.Shutdown()
|
|
ch := n.Subscribe(context.Background(), blockSent.Key())
|
|
|
|
n.Publish(blockSent)
|
|
blockRecvd, ok := <-ch
|
|
if !ok {
|
|
t.Fail()
|
|
}
|
|
|
|
assertBlocksEqual(t, blockRecvd, blockSent)
|
|
|
|
}
|
|
|
|
func TestCarryOnWhenDeadlineExpires(t *testing.T) {
|
|
|
|
impossibleDeadline := time.Nanosecond
|
|
fastExpiringCtx, _ := context.WithTimeout(context.Background(), impossibleDeadline)
|
|
|
|
n := New()
|
|
defer n.Shutdown()
|
|
blockChannel := n.Subscribe(fastExpiringCtx, getBlockOrFail(t, "A Missed Connection").Key())
|
|
|
|
assertBlockChannelNil(t, blockChannel)
|
|
}
|
|
|
|
func assertBlockChannelNil(t *testing.T, blockChannel <-chan *blocks.Block) {
|
|
blockReceived := <-blockChannel
|
|
if blockReceived != nil {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func assertBlocksEqual(t *testing.T, a, b *blocks.Block) {
|
|
if !bytes.Equal(a.Data, b.Data) {
|
|
t.Fail()
|
|
}
|
|
if a.Key() != b.Key() {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func getBlockOrFail(t *testing.T, msg string) *blocks.Block {
|
|
block, blockCreationErr := blocks.NewBlock([]byte(msg))
|
|
if blockCreationErr != nil {
|
|
t.Fail()
|
|
}
|
|
return block
|
|
}
|