-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #457 from bloxapp/stage
Stage to Main (v0.1.7 preparation)
- Loading branch information
Showing
20 changed files
with
446 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"github.com/prysmaticlabs/prysm/async/event" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zaptest" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBroadcaster(t *testing.T) { | ||
logger := zaptest.NewLogger(t) | ||
b := newBroadcaster(logger) | ||
|
||
feed := new(event.Feed) | ||
go func() { | ||
require.NoError(t, b.FromFeed(feed)) | ||
}() | ||
bm1 := newBroadcastedMock("1") | ||
bm2 := newBroadcastedMock("2") | ||
|
||
require.True(t, b.Register(bm1)) | ||
defer b.Deregister(bm1) | ||
|
||
require.True(t, b.Register(bm2)) | ||
|
||
// wait so setup will be finished | ||
<-time.After(10 * time.Millisecond) | ||
go feed.Send(Message{Type: TypeValidator, Filter: MessageFilter{From: 0, To: 0}}) | ||
<-time.After(5 * time.Millisecond) | ||
go b.Deregister(bm2) | ||
<-time.After(5 * time.Millisecond) | ||
go feed.Send(Message{Type: TypeValidator, Filter: MessageFilter{From: 0, To: 0}}) | ||
|
||
// wait so messages will propagate | ||
<-time.After(100 * time.Millisecond) | ||
require.Equal(t, bm1.Size(), 2) | ||
// the second broadcasted was deregistered after the first message | ||
require.Equal(t, bm2.Size(), 1) | ||
} | ||
|
||
type broadcastedMock struct { | ||
mut sync.Mutex | ||
msgs [][]byte | ||
id string | ||
} | ||
|
||
func newBroadcastedMock(id string) *broadcastedMock { | ||
return &broadcastedMock{ | ||
mut: sync.Mutex{}, | ||
msgs: [][]byte{}, | ||
id: id, | ||
} | ||
} | ||
|
||
func (b *broadcastedMock) ID() string { | ||
return b.id | ||
} | ||
|
||
func (b *broadcastedMock) Send(msg []byte) { | ||
b.mut.Lock() | ||
defer b.mut.Unlock() | ||
fmt.Println("sent") | ||
b.msgs = append(b.msgs, msg) | ||
} | ||
|
||
func (b *broadcastedMock) Size() int { | ||
b.mut.Lock() | ||
defer b.mut.Unlock() | ||
|
||
return len(b.msgs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.