Exemple #1
0
// ContextMiddleware creates a new go.net/context and
// injects into the current goji context.
func ContextMiddleware(c *web.C, h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		var ctx = context.Background()
		ctx = datastore.NewContext(ctx, database.NewDatastore(db))
		ctx = blobstore.NewContext(ctx, database.NewBlobstore(db))
		ctx = pool.NewContext(ctx, workers)
		ctx = director.NewContext(ctx, worker)
		ctx = pubsub.NewContext(ctx, pub)

		// add the context to the goji web context
		webcontext.Set(c, ctx)
		h.ServeHTTP(w, r)
	}
	return http.HandlerFunc(fn)
}
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)
		})
	})
}