Merge branch 'master' of github.com:simple-rules/harmony-benchmark

pull/76/head
ak 6 years ago
commit 1f489f3776
  1. 23
      p2p/ida/errors.go
  2. 34
      p2p/ida/ida.go
  3. 77
      p2p/ida/ida_test.go
  4. 4
      p2p/ida/interface.go

@ -0,0 +1,23 @@
// Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Package errors provides common error types used throughout leveldb.
package ida
import (
"errors"
)
// Common errors.
var (
ErrRaptorImpNotFound = New("raptor implementation: not found")
ErrTimeOut = New("timeout: time's up now")
)
// New returns an error that formats as the given text.
func New(text string) error {
return errors.New(text)
}

@ -1,11 +1,37 @@
package ida package ida
// HarmonyIDA implements IDA interface. import (
type HarmonyIDA struct { "time"
raptorQImp *RaptorQ
"github.com/simple-rules/harmony-benchmark/p2p"
)
// IDAImp implements IDA interface.
type IDAImp struct {
raptorQImp RaptorQ
} }
// TakeRaptorQ takes RaptorQ implementation. // TakeRaptorQ takes RaptorQ implementation.
func (ida *HarmonyIDA) TakeRaptorQ(raptorQImp *RaptorQ) { func (ida *IDAImp) TakeRaptorQ(raptorQImp RaptorQ) {
ida.raptorQImp = raptorQImp ida.raptorQImp = raptorQImp
} }
// Process implements very simple IDA logic.
func (ida *IDAImp) Process(msg Message, peers []p2p.Peer, done chan struct{}, timeout time.Duration) error {
if ida.raptorQImp == nil {
return ErrRaptorImpNotFound
}
chunkStream := ida.raptorQImp.Process(msg)
id := 0
for {
select {
case <-done:
return nil
case <-time.After(timeout):
return ErrTimeOut
case chunk := <-chunkStream:
p2p.SendMessage(peers[id], chunk)
id++
}
}
}

@ -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")
// }
}

@ -2,6 +2,7 @@ package ida
import ( import (
"github.com/simple-rules/harmony-benchmark/p2p" "github.com/simple-rules/harmony-benchmark/p2p"
"time"
) )
// Symbol is produced from a RaptorQ implementation. // Symbol is produced from a RaptorQ implementation.
@ -18,5 +19,6 @@ type RaptorQ interface {
// IDA interface. // IDA interface.
type IDA interface { type IDA interface {
Process(msg Message, peers []p2p.Peer, timeout int) TakeRaptorQ(raptorQImp *RaptorQ)
Process(msg Message, peers []p2p.Peer, done chan struct{}, timeout time.Duration) error
} }

Loading…
Cancel
Save