// TestConsumer ensures the consumers main loop properly handles tasks as well
// as errors and Shutdown.
TestConsumer ensures the consumers main loop properly handles tasks as well
as errors and Shutdown.
// TestConsumer ensures the consumers main loop properly handles tasks as well // as errors and Shutdown .
func TestConsumer(t *testing.T) {
	t.Parallel()
	tc := NewTestCoord()
	tc.Tasks <- testTask{"test1"}
	tc.Tasks <- testTask{"test2"}
	hf, tasksRun := newTestHandlerFunc(t)
	c, _ := NewConsumer(tc, hf, DumbBalancer)
	s := make(chan int)
	go func() {
		c.Run()
		s <- 1
	}()
	for i := 0; i < 2; i++ {
		select {
		case <-s:
			t.Fatalf("Run exited early")
		case tr := <-tasksRun:
			if tr != "test1" && tr != "test2" {
				t.Errorf("Expected `test1` or `test2` but received: %s", tr)
			}
			t.Logf("Received task=%q", tr)
		case <-time.After(100 * time.Millisecond):
			t.Errorf("First task didn't execute in a timely fashion")
		}
	}
	tasks := c.Tasks()
	if len(tasks) != 2 {
		t.Errorf("Expected 2 tasks to be running but found: %v", tasks)
	}
	go func() {
		c.Shutdown()
		s <- 1
	}()
	for i := 0; i < 2; i++ {
		select {
		case <-s:
		case <-time.After(100 * time.Millisecond):
			t.Errorf("Run and Shutdown didn't finish in a timely fashion")
		}
	}
}
func TestConsumer ( t * testing . T ) { t . Parallel ( ) tc := NewTestCoord ( ) tc . Tasks <- testTask { " " } tc . Tasks <- testTask { " " } hf , tasksRun := newTestHandlerFunc ( t ) c , _ := NewConsumer ( tc , hf , DumbBalancer ) s := make ( chan int ) go func ( ) { c . Run ( ) s <- 1 } ( ) for i := 0 ; i < 2 ; i ++ { select { case <- s : t . Fatalf ( " " ) case tr := <- tasksRun : if tr != " " && tr != " " { t . Errorf ( " " , tr ) } t . Logf ( " " , tr ) case <- time . After ( 100 * time . Millisecond ) : t . Errorf ( " " ) } } tasks := c . Tasks ( ) if len ( tasks ) != 2 { t . Errorf ( " " , tasks ) } go func ( ) { c . Shutdown ( ) s <- 1 } ( ) for i := 0 ; i < 2 ; i ++ { select { case <- s : case <- time . After ( 100 * time . Millisecond ) : t . Errorf ( " " ) } } }
TestConsumer ensures the consumers main loop properly handles tasks as well
as errors and Shutdown.
TestConsumer ensures the consumers main loop properly handles tasks as well as errors and Shutdown .
// Setup some tasks to run in a fake coordinator // Setup a handler func that lets us know what tasks are running // Create the consumer and run it // Ensure Tasks() is accurate