Skip to content

Remove InputGuard, refactor test components without it #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions component.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,3 @@ func Run(c Component) Wait {

return wait
}

// InputGuard counts number of closed inputs
type InputGuard struct {
ports map[string]bool
complete int
}

// NewInputGuard returns a guard for a given number of inputs
func NewInputGuard(ports ...string) *InputGuard {
portMap := make(map[string]bool, len(ports))
for _, p := range ports {
portMap[p] = false
}

return &InputGuard{portMap, 0}
}

// Complete is called when a port is closed and returns true when all the ports have been closed
func (g *InputGuard) Complete(port string) bool {
if !g.ports[port] {
g.ports[port] = true
g.complete++
}

return g.complete >= len(g.ports)
}
47 changes: 23 additions & 24 deletions components_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ type adder struct {
}

func (c *adder) Process() {
guard := NewInputGuard("op1", "op2")

op1Buf := make([]int, 0, 10)
op2Buf := make([]int, 0, 10)
addOp := func(op int, buf, otherBuf *[]int) {
Expand All @@ -49,21 +47,22 @@ func (c *adder) Process() {
}
}

for {
for c.Op1 != nil || c.Op2 != nil {
select {
case op1, ok := <-c.Op1:
if ok {
addOp(op1, &op1Buf, &op2Buf)
} else if guard.Complete("op1") {
return
if !ok {
c.Op1 = nil
break
}

addOp(op1, &op1Buf, &op2Buf)
case op2, ok := <-c.Op2:
if ok {
addOp(op2, &op2Buf, &op1Buf)
} else if guard.Complete("op2") {
return
if !ok {
c.Op2 = nil
break
}

addOp(op2, &op2Buf, &op1Buf)
}
}
}
Expand All @@ -89,27 +88,27 @@ type repeater struct {
}

func (c *repeater) Process() {
guard := NewInputGuard("word", "times")

times := 0
word := ""

for {
for c.Times != nil || c.Word != nil {
select {
case t, ok := <-c.Times:
if ok {
times = t
c.repeat(word, times)
} else if guard.Complete("times") {
return
if !ok {
c.Times = nil
break
}

times = t
c.repeat(word, times)
case w, ok := <-c.Word:
if ok {
word = w
c.repeat(word, times)
} else if guard.Complete("word") {
return
if !ok {
c.Word = nil
break
}

word = w
c.repeat(word, times)
}
}
}
Expand Down