From 582cc2507a7bef1573946c09137c37591c485c5e Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Tue, 13 Nov 2018 23:22:42 -0800 Subject: [PATCH] add test --- p2p/ida/ida_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++ p2p/ida/interface.go | 3 +- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 p2p/ida/ida_test.go diff --git a/p2p/ida/ida_test.go b/p2p/ida/ida_test.go new file mode 100644 index 000000000..857bc897a --- /dev/null +++ b/p2p/ida/ida_test.go @@ -0,0 +1,77 @@ +package ida + +import ( + "math/rand" + "testing" + "time" + + "github.com/simple-rules/harmony-benchmark/p2p" +) + +var ( + a = Symbol{0, 0} + b = Symbol{1, 0} + c = Symbol{0, 1} + d = Symbol{1, 1} +) + +type FakeRaptor struct { + symbols []Symbol + done chan struct{} + r *rand.Rand +} + +func (raptor *FakeRaptor) Init() { + // raptor.symbols = make([]Symbol, 4) + raptor.symbols = make([]Symbol, 4) + raptor.symbols[0] = make(Symbol, 2) + copy(raptor.symbols[0], a) + raptor.symbols[1] = make(Symbol, 2) + copy(raptor.symbols[1], b) + raptor.symbols[2] = make(Symbol, 2) + copy(raptor.symbols[2], c) + raptor.symbols[3] = make(Symbol, 2) + copy(raptor.symbols[3], d) + raptor.r = rand.New(rand.NewSource(99)) + raptor.done = make(chan struct{}) +} + +func (raptor *FakeRaptor) generate(res chan Symbol) { + for { + select { + case <-raptor.done: + return + default: + i := raptor.r.Intn(4) + res <- raptor.symbols[i] + } + } +} + +func (raptor *FakeRaptor) Process(msg Message) chan Symbol { + res := make(chan Symbol) + go raptor.generate(res) + return res +} + +func TestShouldReturnErrRaptorImpNotFound(t *testing.T) { + ida := &IDAImp{} + done := make(chan struct{}) + err := ida.Process([]byte{}, []p2p.Peer{}, done, time.Second) + if err != ErrRaptorImpNotFound { + t.Fatal("Should return an error") + } +} + +func TestSimple(t *testing.T) { + raptor := &FakeRaptor{} + raptor.Init() + + // ida := &IDAImp{} + // done := make(chan struct{}) + // ida.TakeRaptorQ(raptor) + // err := ida.Process([]byte{}, []p2p.Peer{}, done, time.Second) + // if err == ErrRaptorImpNotFound { + // t.Fatal("Should return an error") + // } +} diff --git a/p2p/ida/interface.go b/p2p/ida/interface.go index 99b0023e0..03d102cc7 100644 --- a/p2p/ida/interface.go +++ b/p2p/ida/interface.go @@ -2,6 +2,7 @@ package ida import ( "github.com/simple-rules/harmony-benchmark/p2p" + "time" ) // Symbol is produced from a RaptorQ implementation. @@ -19,5 +20,5 @@ type RaptorQ interface { // IDA interface. type IDA interface { TakeRaptorQ(raptorQImp *RaptorQ) - Process(msg Message, peers []p2p.Peer, timeout int) + Process(msg Message, peers []p2p.Peer, done chan struct{}, timeout time.Duration) error }