Skip to content

Commit

Permalink
Merge pull request #15 from it-chain/feature/avengers
Browse files Browse the repository at this point in the history
implement network delay property
  • Loading branch information
frontalnh authored Oct 6, 2018
2 parents f890361 + e1bfc90 commit 03bb78b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 50 deletions.
84 changes: 55 additions & 29 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion mock/event_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@ package mock
import (
"errors"
"reflect"
"time"
)

var ErrEventType = errors.New("Error type of event is not struct")

type EventService struct {
ProcessId string
PublishFunc func(processId string, topic string, event interface{}) error
delayTime time.Duration
}

func NewEventService(processId string, publishFunc func(processId string, topic string, event interface{}) error) *EventService {
return &EventService{
ProcessId: processId,
PublishFunc: publishFunc,
delayTime: 0,
}
}

func (s *EventService) Publish(topic string, event interface{}) error {

time.Sleep(s.delayTime)
if !eventIsStruct(event) {
return ErrEventType
}
Expand All @@ -48,7 +52,11 @@ func (s *EventService) Publish(topic string, event interface{}) error {
return nil
}

func (s *EventService) Close(){}
func (s *EventService) SetDelayTime(t time.Duration) {
s.delayTime = t
}

func (s *EventService) Close() {}

func eventIsStruct(event interface{}) bool {
return reflect.TypeOf(event).Kind() == reflect.Struct
Expand Down
10 changes: 7 additions & 3 deletions mock/event_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ package mock_test

import (
"testing"
"time"

"github.com/it-chain/avengers/mock"
"github.com/it-chain/engine/common/command"
)

func TestEventService_Publish(t *testing.T) {
eventService:=mock.NewEventService("1", func(processId string, topic string, event interface{}) error {
eventService := mock.NewEventService("1", func(processId string, topic string, event interface{}) error {
return nil
})

eventService.SetDelayTime(5 * time.Millisecond)

event := command.DeliverGrpc{
MessageId:"1",
MessageId: "1",
}

eventService.Publish("message.deliver",event)
eventService.Publish("message.deliver", event)
}
17 changes: 0 additions & 17 deletions mock/rpc_test.go

This file was deleted.

0 comments on commit 03bb78b

Please sign in to comment.