Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Commit

Permalink
Fix remove peer from workers heap
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed May 20, 2019
1 parent fc7a6c8 commit 1b541ab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
13 changes: 9 additions & 4 deletions protocol/ethereum/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ func NewBackend(minimal *minimal.Minimal, blockchain *blockchain.Blockchain) (*B
waitCh: make([]chan struct{}, 0),

// -- new fields
heap: &workersHeap{
index: map[string]*worker{},
heap: workersHeapImpl{},
},
heap: newWorkersHeap(),
tasks: map[uint64]*task{},
tasksLock: sync.Mutex{},
wakeCh: make(chan struct{}, 10),
Expand Down Expand Up @@ -558,6 +555,13 @@ type worker struct {
proto *Ethereum
}

func newWorkersHeap() *workersHeap {
return &workersHeap{
index: map[string]*worker{},
heap: workersHeapImpl{},
}
}

type workersHeap struct {
index map[string]*worker
heap workersHeapImpl
Expand All @@ -568,6 +572,7 @@ func (w *workersHeap) Remove(id string) {
if !ok {
return
}
delete(w.index, id)
heap.Remove(&w.heap, wrk.index)
return
}
Expand Down
19 changes: 19 additions & 0 deletions protocol/ethereum/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,22 @@ func TestBackendNotify(t *testing.T) {
fmt.Println("-- forks --")
fmt.Println(b.GetForks())
}

func TestWorkerPool(t *testing.T) {
pool := newWorkersHeap()

// pool is nil the first time
assert.Nil(t, pool.Peek())

// push and peek
assert.NoError(t, pool.Push("1", nil))
assert.Equal(t, pool.Peek().id, "1")

assert.NoError(t, pool.Push("2", nil))
assert.Len(t, pool.index, 2)

// Remove '1'. Only one element left
pool.Remove("1")
assert.Len(t, pool.index, 1)
assert.Equal(t, pool.Peek().id, "2")
}

0 comments on commit 1b541ab

Please sign in to comment.