func Test_RecoverJob_NilActiveSaga(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	sc, slog := makeMockSagaCoord(mockCtrl)
	job := domain.GenJob("sagaId", 10)
	jobData, _ := (&job).Serialize()
	slog.EXPECT().GetActiveSagas().Return([]string{"saga1", "saga2"}, nil)
	slog.EXPECT().GetMessages("saga1").Return([]saga.SagaMessage{
		saga.MakeStartSagaMessage("saga1", jobData),
	}, nil)
	slog.EXPECT().GetMessages("saga2").Return(nil, nil)
	addJobCh := make(chan jobAddedMsg, 5)
	recoverJobs(sc, addJobCh)
	recoveredJobs := make(map[string]jobAddedMsg)
	for i := 0; i < 2; i++ {
		select {
		case msg := <-addJobCh:
			recoveredJobs[msg.saga.GetState().SagaId()] = msg
		default:
		}
	}
	if _, ok1 := recoveredJobs["saga1"]; !ok1 {
		t.Errorf("Expected saga1 to be rescheduled")
	}
	if _, ok2 := recoveredJobs["saga2"]; ok2 {
		t.Errorf("expected saga2 to be not be rescheduled")
	}
}
verifies that if recovering saga returns nil, then we don't add it
to the addJobs channel
package server
import (
	"errors"
	"github.com/golang/mock/gomock"
	"github.com/twitter/scoot/saga"
	"github.com/twitter/scoot/scheduler/domain"
	"testing"
	"time"
)
func makeMockSagaCoord(mockCtrl *gomock.Controller) (saga.SagaCoordinator, *saga.MockSagaLog) {
	sagaLogMock := saga.NewMockSagaLog(mockCtrl)
	sagaCoord := saga.MakeSagaCoordinator(sagaLogMock, nil)
	return sagaCoord, sagaLogMock
}
func Test_RecoverJobs_NilActiveSagas(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	sc, slog := makeMockSagaCoord(mockCtrl)
	slog.EXPECT().GetActiveSagas().Return(nil, nil)
	addJobCh := make(chan jobAddedMsg, 1)
	recoverJobs(sc, addJobCh)
	select {
	case msg := <-addJobCh:
		t.Errorf("unexpected message added to addJobCh %v", msg)
	default:
	}
}
func Test_RecoverJobs_EmptyActiveSagas(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	sc, slog := makeMockSagaCoord(mockCtrl)
	slog.EXPECT().GetActiveSagas().Return([]string{}, nil)
	addJobCh := make(chan jobAddedMsg, 1)
	recoverJobs(sc, addJobCh)
	select {
	case msg := <-addJobCh:
		t.Errorf("unexpected message added to addJobCh %v", msg)
	default:
	}
}
func Test_RecoverJobs_GetActiveSagasFails(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	sc, slog := makeMockSagaCoord(mockCtrl)
	gomock.InOrder(
		slog.EXPECT().GetActiveSagas().Return(nil, errors.New("Test Error!")),
		slog.EXPECT().GetActiveSagas().Return(nil, errors.New("Test Error Again!")),
		slog.EXPECT().GetActiveSagas().Return(nil, errors.New("Omg so many Test Errors!")),
		slog.EXPECT().GetActiveSagas().Return([]string{}, nil),
	)
	addJobCh := make(chan jobAddedMsg, 1)
	recoverJobs(sc, addJobCh)
}
func Test_RecoverJobs_ActiveSagas(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()
	sc, slog := makeMockSagaCoord(mockCtrl)
	job1 := domain.GenJob("saga1", 10)
	job2 := domain.GenJob("saga2", 5)
	job1Data, _ := (&job1).Serialize()
	job2Data, _ := (&job2).Serialize()
	slog.EXPECT().GetActiveSagas().Return([]string{"saga1", "saga2"}, nil)
	slog.EXPECT().GetMessages("saga1").Return([]saga.SagaMessage{
		saga.MakeStartSagaMessage("saga1", job1Data),
	}, nil)
	slog.EXPECT().GetMessages("saga2").Return([]saga.SagaMessage{
		saga.MakeStartSagaMessage("saga2", job2Data),
	}, nil)
	addJobCh := make(chan jobAddedMsg, 5)
	recoverJobs(sc, addJobCh)
	recoveredJobs := make(map[string]jobAddedMsg)
	for i := 0; i < 2; i++ {
		select {
		case msg := <-addJobCh:
			recoveredJobs[msg.saga.GetState().SagaId()] = msg
		default:
		}
	}
	if _, ok1 := recoveredJobs["saga1"]; !ok1 {
		t.Errorf("Expected saga1 to be rescheduled")
	}
	if _, ok2 := recoveredJobs["saga2"]; !ok2 {
		t.Errorf("expected saga2 to be rescheduled")
	}
}