2016-12-08 16:24:38 +00:00
|
|
|
package ipfscluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2016-12-09 19:54:46 +00:00
|
|
|
|
2016-12-16 11:40:28 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
2016-12-08 16:24:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-12-09 19:54:46 +00:00
|
|
|
testCid = "QmP63DkAFEnDYNjDYBpyNDfttu1fvUw99x1brscPzpqmmq"
|
|
|
|
testCid2 = "QmP63DkAFEnDYNjDYBpyNDfttu1fvUw99x1brscPzpqmma"
|
|
|
|
testCid3 = "QmP63DkAFEnDYNjDYBpyNDfttu1fvUw99x1brscPzpqmmb"
|
|
|
|
testPeerID, _ = peer.IDB58Decode("QmXZrtE5jQwXNqCJMfHUTQkvhQ4ZAnqMnmzFMJfLewuabc")
|
2016-12-08 16:24:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMakeRPC(t *testing.T) {
|
2016-12-15 18:08:46 +00:00
|
|
|
testCh := make(chan RPC, 1)
|
|
|
|
testReq := NewRPC(MemberListRPC, nil)
|
2016-12-08 16:24:38 +00:00
|
|
|
testResp := RPCResponse{
|
|
|
|
Data: "hey",
|
|
|
|
Error: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// Test wait for response
|
|
|
|
go func() {
|
|
|
|
req := <-testCh
|
|
|
|
if req.Op() != MemberListRPC {
|
|
|
|
t.Fatal("expected a MemberListRPC")
|
|
|
|
}
|
|
|
|
req.ResponseCh() <- testResp
|
|
|
|
}()
|
|
|
|
|
|
|
|
resp := MakeRPC(ctx, testCh, testReq, true)
|
|
|
|
data, ok := resp.Data.(string)
|
|
|
|
if !ok || data != "hey" {
|
|
|
|
t.Error("bad response")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test not waiting for response
|
|
|
|
resp = MakeRPC(ctx, testCh, testReq, false)
|
|
|
|
if resp.Data != nil || resp.Error != nil {
|
|
|
|
t.Error("expected empty response")
|
|
|
|
}
|
|
|
|
|
2016-12-14 16:55:42 +00:00
|
|
|
// Test full channel and cancel on send
|
2016-12-08 16:24:38 +00:00
|
|
|
go func() {
|
|
|
|
resp := MakeRPC(ctx, testCh, testReq, true)
|
|
|
|
if resp.Error == nil || !strings.Contains(resp.Error.Error(), "timed out") {
|
|
|
|
t.Fatal("the operation should have been waiting and then cancelled")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// Previous request still taking the channel
|
2016-12-14 16:55:42 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2016-12-08 16:24:38 +00:00
|
|
|
cancel()
|
2016-12-14 16:55:42 +00:00
|
|
|
|
|
|
|
// Test cancelled while waiting for response context
|
|
|
|
ctx, cancel = context.WithCancel(context.Background())
|
2016-12-15 18:08:46 +00:00
|
|
|
testCh = make(chan RPC, 1)
|
2016-12-14 16:55:42 +00:00
|
|
|
go func() {
|
|
|
|
resp := MakeRPC(ctx, testCh, testReq, true)
|
|
|
|
if resp.Error == nil || !strings.Contains(resp.Error.Error(), "timed out") {
|
|
|
|
t.Fatal("the operation should have been waiting and then cancelled")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
cancel()
|
|
|
|
time.Sleep(1 * time.Second)
|
2016-12-08 16:24:38 +00:00
|
|
|
}
|
2016-12-14 14:31:50 +00:00
|
|
|
|
2016-12-15 18:08:46 +00:00
|
|
|
func simulateAnswer(ch <-chan RPC, answer interface{}, err error) {
|
2016-12-14 14:31:50 +00:00
|
|
|
go func() {
|
|
|
|
req := <-ch
|
|
|
|
req.ResponseCh() <- RPCResponse{
|
|
|
|
Data: answer,
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|