-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtask_test.go
92 lines (76 loc) · 1.93 KB
/
task_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gossamr
import (
"fmt"
"github.com/markchadwick/spec"
"io"
"sync"
)
type Echo struct {
}
func (e *Echo) Map(k, v string, c Collector) error {
return c.Collect(fmt.Sprintf("%s said", k), fmt.Sprintf("Hello, %s", v))
}
func (e *Echo) String() string {
return "Echo"
}
var _ = spec.Suite("Task", func(c *spec.C) {
echo := new(Echo)
echoTask := NewTask(echo)
w := NewTestBuffer()
r := NewTestBuffer()
defer r.Close()
defer w.Close()
c.It("should know when a method is missing", func(c *spec.C) {
_, ok := echoTask.methodByName("Missing")
c.Assert(ok).IsFalse()
})
c.It("should know when a method exists", func(c *spec.C) {
mapper, ok := echoTask.methodByName("Map")
c.Assert(ok).IsTrue()
c.Assert(mapper).NotNil()
})
c.It("should not run an invalid phase", func(c *spec.C) {
err := echoTask.Run(66, r, w)
c.Assert(err).NotNil()
c.Assert(err.Error()).Equals("Invalid phase 66")
})
c.It("should not run an unimplemented phase", func(c *spec.C) {
err := echoTask.Run(CombinePhase, r, w)
c.Assert(err).NotNil()
c.Assert(err.Error()).Equals("No phase 1 for Echo")
})
c.It("should run a simple map phase", func(c *spec.C) {
input := NewPairWriter(r)
output := NewPairReader(w)
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
defer wg.Done()
c.Assert(input.Write("thelma", "louise"))
c.Assert(input.Write("abbott", "costello"))
input.Close()
}()
wg.Add(1)
go func() {
defer wg.Done()
err := echoTask.Run(MapPhase, r, w)
c.Assert(err).IsNil()
}()
var k, v interface{}
var err error
k, v, err = output.Next()
c.Assert(err).IsNil()
c.Assert(k).Equals("thelma said")
c.Assert(v).Equals("Hello, louise")
k, v, err = output.Next()
c.Assert(err).IsNil()
c.Assert(k).Equals("abbott said")
c.Assert(v).Equals("Hello, costello")
k, v, err = output.Next()
c.Assert(k).IsNil()
c.Assert(v).IsNil()
c.Assert(err).NotNil()
c.Assert(err).Equals(io.EOF)
wg.Wait()
})
})