Beispiel #1
0
// Sends a single action to action channel
func SendEventActionToJobChannel(workerPool chan *Worker, worker *Worker, action *EventAction) *Worker {
	expBuffer, _ := dialects.ConvertJSON(action.Event)
	exp[expBuffer.String()] = struct{}{}
	worker.JobChannel <- action
	worker = <-workerPool
	return worker
}
Beispiel #2
0
// Multiple worker tests for simple storage client
func TestSimpleStorageClientMultipleWorker(t *testing.T) {
	storageClient = &SimpleStorageClient{} // Define the Buffer Storage as a storage
	jobQueue = make(chan Job, 10)          // Creates a jobQueue
	log.SetOutput(ioutil.Discard)          // Disable the logger
	T, response, catched = t, nil, false   // Set properties

	t.Log("Creating two workers to compete with each other")
	pool := make(chan *Worker, 2)
	w1 := NewWorker(1, &WorkerOptions{RetryAttempt: 3}, pool)
	w1.Start()

	w2 := NewWorker(2, &WorkerOptions{RetryAttempt: 3}, pool)
	w2.Start()

	t.Log("Create two actions")
	action1 := EventAction{GetTestEvent(1262473173), 1}
	expBuffer1, _ := dialects.ConvertJSON(action1.Event)

	action2 := EventAction{GetTestEvent(53484332), 1}
	expBuffer2, _ := dialects.ConvertJSON(action2.Event)

	exp = map[string]struct{}{expBuffer1.String(): {}, expBuffer2.String(): {}}

	t.Log("Send the actions to different workers")
	worker := <-pool
	worker.JobChannel <- &action1
	worker = <-pool
	worker.JobChannel <- &action2

	t.Log("Get channel to wait until the previous actions are finished")
	<-pool
	<-pool
	if !catched {
		t.Errorf("Worker didn't catch the expected actions")
	}

	t.Log("Stop the worker on the end")
	var wg sync.WaitGroup
	wg.Add(2)
	w1.Stop(&wg)
	w2.Stop(&wg)
	wg.Wait()
}
Beispiel #3
0
// Testing the dispatcher listen function
func TestDispatcherListen(t *testing.T) {
	config = &Config{}                     // Define an empty config
	storageClient = &SimpleStorageClient{} // Define the Simple Storage as a storage
	jobQueue = make(chan Job, 10)          //Define the job Queue
	log.SetOutput(ioutil.Discard)          // Disable the logger
	T, response, catched = t, nil, false   // Set properties

	t.Log("Creates the dispatcher and listen for new jobs")
	options := &WorkerOptions{RetryAttempt: 5}
	dispatcher := NewDispatcher(2, options)
	dispatcher.Run()

	if exp := 2; len(dispatcher.Workers) != exp {
		t.Errorf("Expected worker's count was %d but it was %d instead", exp, len(dispatcher.Workers))
	}

	t.Log("Creating two jobs and put it into the job queue")
	job1 := EventAction{GetTestEvent(423432), 1}
	expBuffer1, _ := dialects.ConvertJSON(job1.Event)

	job2 := EventAction{GetTestEvent(7643329), 1}
	expBuffer2, _ := dialects.ConvertJSON(job2.Event)

	t.Log("It should catch a different worker with the expected results")
	exp = map[string]struct{}{expBuffer1.String(): {}, expBuffer2.String(): {}}
	jobQueue <- &job1
	jobQueue <- &job2

	t.Log("Wait until both is finished")
	time.Sleep(150 * time.Millisecond)
	if !catched {
		t.Errorf("Worker didn't catch the expected jobs")
	}

	t.Log("Stops the workers")
	dispatcher.Stop()
}
Beispiel #4
0
// Sets the actions expectation
func SetEventExpectation(eventActions []*EventAction, fail bool, resetExpectation bool) {
	if fail {
		response = fmt.Errorf("Error was intialized for testing")
	} else {
		response = nil
	}
	if resetExpectation {
		exp = map[string]struct{}{}
	}
	var part *bytes.Buffer
	partStr := ""
	for _, action := range eventActions {
		part, _ = dialects.ConvertJSON(action.Event)
		partStr += part.String()
	}
	exp[partStr] = struct{}{}
}
Beispiel #5
0
// Testing the dispatcher listen function
func TestDispatcherAutomaticFlush(t *testing.T) {
	config = &Config{AutoFlushInterval: 3}   // Define the config
	storageClient = &BufferedStorageClient{} // Define the Buffered Storage as a storage
	jobQueue = make(chan Job, 10)            //Define the job Queue
	log.SetOutput(ioutil.Discard)            // Disable the logger
	T, response, catched = t, nil, false     // Set properties

	t.Log("Creates the dispatcher with a single worker and listen for new jobs")
	options := &WorkerOptions{BufferSize: 3}
	dispatcher := NewDispatcher(1, options)
	dispatcher.Run()

	if exp := 1; len(dispatcher.Workers) != exp {
		t.Errorf("Expected worker's count was %d but it was %d instead", exp, len(dispatcher.Workers))
	}

	t.Log("Creating a job and put it into the job queue")
	job := EventAction{GetTestEvent(636284), 1}
	expBuffer, _ := dialects.ConvertJSON(job.Event)

	exp = map[string]struct{}{expBuffer.String(): {}}
	jobQueue <- &job

	t.Log("Wait until it's finished, it should be buffered and not saved")
	time.Sleep(150 * time.Millisecond)
	if catched {
		t.Errorf("Worker shouldn't catch the job")
	}

	t.Log("Run an automatic flush")
	dispatcher.Flush(&FlushOptions{Automatic: true})

	t.Log("Wait and check the flush isn't finished, it should not be fired")
	time.Sleep(150 * time.Millisecond)
	if catched {
		t.Errorf("Worker shouldn't catch the job")
	}

	t.Log("Wait until the interval is over (started by the dispatcher) and check again")
	time.Sleep(3 * time.Second)
	if !catched {
		t.Errorf("Worker didn't catch the job")
	}
}