Example #1
0
// GetWorkAssigned accepts a request to retrieve the list
// of started work and returns in JSON format.
//
//     GET /api/work/assignments
//
func GetWorkAssigned(c web.C, w http.ResponseWriter, r *http.Request) {
	ctx := context.FromC(c)
	d := worker.FromContext(ctx).(*director.Director)
	json.NewEncoder(w).Encode(d.GetAssignemnts())
}
Example #2
0
func TestDirector(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("Director", func() {

		g.It("Should mark work as pending", func() {
			d := New()
			d.markPending(&worker.Work{})
			d.markPending(&worker.Work{})
			g.Assert(len(d.GetPending())).Equal(2)
		})

		g.It("Should mark work as started", func() {
			d := New()
			w1 := worker.Work{}
			w2 := worker.Work{}
			d.markPending(&w1)
			d.markPending(&w2)
			g.Assert(len(d.GetPending())).Equal(2)
			d.markStarted(&w1, &mockWorker{})
			g.Assert(len(d.GetStarted())).Equal(1)
			g.Assert(len(d.GetPending())).Equal(1)
			d.markStarted(&w2, &mockWorker{})
			g.Assert(len(d.GetStarted())).Equal(2)
			g.Assert(len(d.GetPending())).Equal(0)
		})

		g.It("Should mark work as complete", func() {
			d := New()
			w1 := worker.Work{}
			w2 := worker.Work{}
			d.markStarted(&w1, &mockWorker{})
			d.markStarted(&w2, &mockWorker{})
			g.Assert(len(d.GetStarted())).Equal(2)
			d.markComplete(&w1)
			g.Assert(len(d.GetStarted())).Equal(1)
			d.markComplete(&w2)
			g.Assert(len(d.GetStarted())).Equal(0)
		})

		g.It("Should get work assignments", func() {
			d := New()
			w1 := worker.Work{}
			w2 := worker.Work{}
			d.markStarted(&w1, &mockWorker{})
			d.markStarted(&w2, &mockWorker{})
			g.Assert(len(d.GetAssignemnts())).Equal(2)
		})

		g.It("Should recover from a panic", func() {
			d := New()
			d.Do(nil, nil)
			g.Assert(true).Equal(true)
		})

		g.It("Should distribute work to worker", func() {
			work := &worker.Work{}
			workr := &mockWorker{}
			c := context.Background()
			p := pool.New()
			p.Allocate(workr)
			c = pool.NewContext(c, p)

			d := New()
			d.do(c, work)
			g.Assert(workr.work).Equal(work) // verify mock worker gets work
		})

		g.It("Should add director to context", func() {
			d := New()
			c := context.Background()
			c = NewContext(c, d)
			g.Assert(worker.FromContext(c)).Equal(d)
		})
	})
}