var _ = spec.Suite("Grouped Reader", func(c *spec.C) { c.It("should know when its input is closed", func(c *spec.C) { tr := &TestReader{} gr := NewGroupedReader(tr) _, _, err := gr.Next() c.Assert(err).Equals(io.EOF) }) c.It("should group adjacent keys", func(c *spec.C) { tr := NewTestReader([][2]interface{}{ [2]interface{}{"seen", 12}, [2]interface{}{"seen", 82}, }) gr := NewGroupedReader(tr) key, vs, err := gr.Next() c.Assert(err).IsNil() c.Assert(key).Equals("seen") ch, ok := vs.(chan int) c.Assert(ok).IsTrue() observed := make([]int, 0) for o := range ch { observed = append(observed, o) } c.Assert(observed).HasLen(2) c.Assert(observed[0]).Equals(12) c.Assert(observed[1]).Equals(82) key, vs, err = gr.Next() c.Assert(err).Equals(io.EOF) c.Assert(key).IsNil() c.Assert(vs).IsNil() }) })
var _ = spec.Suite("Task Phase Runner", func(c *spec.C) { c.It("should bail with missing args", func(c *spec.C) { args := []string{ "./myprog", } _, err := TaskPhaseRunnerFromArgs(args) c.Assert(err).NotNil() }) c.It("should bail on invalid phase", func(c *spec.C) { args := []string{ "./myprog", "-phase", "rock it", } _, err := TaskPhaseRunnerFromArgs(args) c.Assert(err).NotNil() c.Assert(err.Error()).Equals("Unknown phase rock it") }) c.It("should parse task # and phase", func(c *spec.C) { args := []string{ "./myprog", "-task", "2", "-phase", "combine", } r, err := TaskPhaseRunnerFromArgs(args) c.Assert(err).IsNil() c.Assert(r.taskNo).Equals(2) c.Assert(r.phase).Equals(CombinePhase) }) })
import ( "fmt" "github.com/markchadwick/spec" "log" "math/rand" ) var _ = spec.Suite("Sort Writer", func(c *spec.C) { c.It("should flipping run", func(c *spec.C) { buf := NewBufCloser() sw, err := NewSortWriter(buf, 10) c.Assert(err).IsNil() for i := 0; i < 25; i++ { key := fmt.Sprintf("rec-%05d", rand.Int31n(100)) c.Assert(sw.Write(key, int32(i))).IsNil() } c.Assert(sw.Close()).IsNil() pr := NewPairReader(buf) for { k, v, err := pr.Next() if err != nil { return } log.Printf("%v = %v", k, v) } }) })
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(err).NotNil() c.Assert(err).Equals(io.EOF) wg.Wait() }) })