Skip to content

Commit 288dea0

Browse files
authored
Merge pull request #64 from akokhanovskyi/noinputguard
Remove InputGuard, refactor test components without it
2 parents 89a5c9e + afe1d5b commit 288dea0

File tree

2 files changed

+23
-50
lines changed

2 files changed

+23
-50
lines changed

component.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,3 @@ func Run(c Component) Wait {
2222

2323
return wait
2424
}
25-
26-
// InputGuard counts number of closed inputs
27-
type InputGuard struct {
28-
ports map[string]bool
29-
complete int
30-
}
31-
32-
// NewInputGuard returns a guard for a given number of inputs
33-
func NewInputGuard(ports ...string) *InputGuard {
34-
portMap := make(map[string]bool, len(ports))
35-
for _, p := range ports {
36-
portMap[p] = false
37-
}
38-
39-
return &InputGuard{portMap, 0}
40-
}
41-
42-
// Complete is called when a port is closed and returns true when all the ports have been closed
43-
func (g *InputGuard) Complete(port string) bool {
44-
if !g.ports[port] {
45-
g.ports[port] = true
46-
g.complete++
47-
}
48-
49-
return g.complete >= len(g.ports)
50-
}

components_for_test.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ type adder struct {
3535
}
3636

3737
func (c *adder) Process() {
38-
guard := NewInputGuard("op1", "op2")
39-
4038
op1Buf := make([]int, 0, 10)
4139
op2Buf := make([]int, 0, 10)
4240
addOp := func(op int, buf, otherBuf *[]int) {
@@ -49,21 +47,22 @@ func (c *adder) Process() {
4947
}
5048
}
5149

52-
for {
50+
for c.Op1 != nil || c.Op2 != nil {
5351
select {
5452
case op1, ok := <-c.Op1:
55-
if ok {
56-
addOp(op1, &op1Buf, &op2Buf)
57-
} else if guard.Complete("op1") {
58-
return
53+
if !ok {
54+
c.Op1 = nil
55+
break
5956
}
6057

58+
addOp(op1, &op1Buf, &op2Buf)
6159
case op2, ok := <-c.Op2:
62-
if ok {
63-
addOp(op2, &op2Buf, &op1Buf)
64-
} else if guard.Complete("op2") {
65-
return
60+
if !ok {
61+
c.Op2 = nil
62+
break
6663
}
64+
65+
addOp(op2, &op2Buf, &op1Buf)
6766
}
6867
}
6968
}
@@ -89,27 +88,27 @@ type repeater struct {
8988
}
9089

9190
func (c *repeater) Process() {
92-
guard := NewInputGuard("word", "times")
93-
9491
times := 0
9592
word := ""
9693

97-
for {
94+
for c.Times != nil || c.Word != nil {
9895
select {
9996
case t, ok := <-c.Times:
100-
if ok {
101-
times = t
102-
c.repeat(word, times)
103-
} else if guard.Complete("times") {
104-
return
97+
if !ok {
98+
c.Times = nil
99+
break
105100
}
101+
102+
times = t
103+
c.repeat(word, times)
106104
case w, ok := <-c.Word:
107-
if ok {
108-
word = w
109-
c.repeat(word, times)
110-
} else if guard.Complete("word") {
111-
return
105+
if !ok {
106+
c.Word = nil
107+
break
112108
}
109+
110+
word = w
111+
c.repeat(word, times)
113112
}
114113
}
115114
}

0 commit comments

Comments
 (0)