Handle channel overflow, test channel data

This commit is contained in:
Ian Gulliver
2023-06-12 19:34:22 -07:00
parent 9a268cada3
commit de0e769834
2 changed files with 30 additions and 1 deletions

View File

@@ -215,7 +215,11 @@ func (c *Candidate) elect(v *vote) {
) )
c.state = state c.state = state
c.c <- state
select {
case c.c <- state:
default:
}
}() }()
if c.forceState != StateUndefined { if c.forceState != StateUndefined {

View File

@@ -4,6 +4,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/gopatchy/elect"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@@ -37,6 +38,10 @@ func TestThree(t *testing.T) {
w.Wait() w.Wait()
} }
require.Equal(t, <-ts.Candidate(0).C, elect.StateLeader)
require.Empty(t, ts.Candidate(1).C)
require.Empty(t, ts.Candidate(2).C)
} }
func TestFailover(t *testing.T) { func TestFailover(t *testing.T) {
@@ -59,6 +64,10 @@ func TestFailover(t *testing.T) {
w.Wait() w.Wait()
} }
require.Equal(t, <-ts.Candidate(0).C, elect.StateLeader)
require.Empty(t, ts.Candidate(1).C)
require.Empty(t, ts.Candidate(2).C)
ts.SetServer(1) ts.SetServer(1)
require.Eventually(t, func() bool { return !ts.Candidate(0).IsLeader() }, 15*time.Second, 100*time.Millisecond) require.Eventually(t, func() bool { return !ts.Candidate(0).IsLeader() }, 15*time.Second, 100*time.Millisecond)
@@ -66,6 +75,10 @@ func TestFailover(t *testing.T) {
require.False(t, ts.Candidate(1).IsLeader()) require.False(t, ts.Candidate(1).IsLeader())
require.False(t, ts.Candidate(2).IsLeader()) require.False(t, ts.Candidate(2).IsLeader())
require.Equal(t, <-ts.Candidate(0).C, elect.StateNotLeader)
require.Empty(t, ts.Candidate(1).C)
require.Empty(t, ts.Candidate(2).C)
{ {
w := NewWaiter() w := NewWaiter()
@@ -75,6 +88,10 @@ func TestFailover(t *testing.T) {
w.Wait() w.Wait()
} }
require.Equal(t, <-ts.Candidate(1).C, elect.StateLeader)
require.Empty(t, ts.Candidate(0).C)
require.Empty(t, ts.Candidate(2).C)
} }
func TestPartialVotes(t *testing.T) { func TestPartialVotes(t *testing.T) {
@@ -98,6 +115,10 @@ func TestPartialVotes(t *testing.T) {
w.Wait() w.Wait()
} }
require.Equal(t, <-ts.Candidate(0).C, elect.StateLeader)
require.Empty(t, ts.Candidate(1).C)
require.Empty(t, ts.Candidate(2).C)
} }
func TestSplitVotes(t *testing.T) { func TestSplitVotes(t *testing.T) {
@@ -122,4 +143,8 @@ func TestSplitVotes(t *testing.T) {
w.Wait() w.Wait()
} }
require.Equal(t, <-ts.Candidate(1).C, elect.StateLeader)
require.Empty(t, ts.Candidate(0).C)
require.Empty(t, ts.Candidate(2).C)
} }