Last active
August 29, 2015 13:59
-
-
Save wingyplus/10623109 to your computer and use it in GitHub Desktop.
Kata RX Vote
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
package rxvote | |
import "time" | |
type TrueVote int | |
func (tv TrueVote) Value() int { | |
return int(tv) | |
} | |
type FalseVote int | |
func (tv FalseVote) Value() int { | |
return int(tv) | |
} | |
func Voter(trueVote, falseVote chan int, votes []bool) { | |
tVote, fVote := 0, 0 | |
for _, v := range votes { | |
if v { | |
tVote++ | |
} else { | |
fVote++ | |
} | |
} | |
trueVote <- tVote | |
falseVote <- fVote | |
} | |
func CollectVote(trueVote TrueVote, falseVote FalseVote) bool { | |
return trueVote.Value() > falseVote.Value() | |
} | |
func ResultVote(votes []bool, timeout time.Duration) bool { | |
trueVote, falseVote := make(chan int), make(chan int) | |
go Voter(trueVote, falseVote, votes) | |
select { | |
case <-time.After(timeout): | |
return CollectVote(TrueVote(<-trueVote), FalseVote(<-falseVote)) | |
} | |
} |
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
package rxvote_test | |
import ( | |
"github.com/wingyplus/rxvote" | |
"testing" | |
"time" | |
) | |
func TestVoterWithOneTrueVote(t *testing.T) { | |
var trueVote, falseVote = make(chan int), make(chan int) | |
go rxvote.Voter(trueVote, falseVote, []bool{true}) | |
var expectTrueVote = <-trueVote | |
if expectTrueVote != 1 { | |
t.Errorf("expect 1 but was %v", expectTrueVote) | |
} | |
} | |
func TestVoterWithOneTrueVoteAndOneFalseVote(t *testing.T) { | |
var trueVote, falseVote = make(chan int), make(chan int) | |
go rxvote.Voter(trueVote, falseVote, []bool{true, false}) | |
expectTrueVote, expectFalseVote := <-trueVote, <-falseVote | |
if expectTrueVote != 1 || expectFalseVote != 1 { | |
t.Errorf("expect true 1 and false 1 but true %v and false %v", expectTrueVote, expectFalseVote) | |
} | |
} | |
func TestVoterWithTwoTrueVoteAndOneFalseVote(t *testing.T) { | |
var trueVote, falseVote = make(chan int), make(chan int) | |
go rxvote.Voter(trueVote, falseVote, []bool{true, false, true}) | |
expectTrueVote, expectFalseVote := <-trueVote, <-falseVote | |
if expectTrueVote != 2 || expectFalseVote != 1 { | |
t.Errorf("expect true 2 and false 1 but true %v and false %v ", expectTrueVote, expectFalseVote) | |
} | |
} | |
func TestCollectVote(t *testing.T) { | |
var result bool = rxvote.CollectVote(rxvote.TrueVote(1), rxvote.FalseVote(0)) | |
if !result { | |
t.Errorf("expect true but was %v", result) | |
} | |
} | |
func TestTrueVote(t *testing.T) { | |
var trueVote = rxvote.TrueVote(1) | |
if trueVote.Value() != 1 { | |
t.Errorf("expect 1 but was %v", trueVote.Value()) | |
} | |
} | |
func TestFalseVote(t *testing.T) { | |
var falseVote = rxvote.FalseVote(1) | |
if falseVote.Value() != 1 { | |
t.Errorf("expect 1 but was %v", falseVote.Value()) | |
} | |
} | |
func TestCollectVoteAfterTimeout(t *testing.T) { | |
var expectTrueVote bool = rxvote.ResultVote([]bool{true, false, true}, 1*time.Second) | |
if !expectTrueVote { | |
t.Errorf("expect true but was %v", expectTrueVote) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment