예제 #1
0
// In normal operation, we expect one access log entry,
// and one data collector entry. Let's assume both will succeed.
// We should return a HTTP 200 status.
func TestCollectSuccessfully(t *testing.T) {
	dataCollectorMock := mocks.NewSyncProducer(t, nil)
	dataCollectorMock.ExpectSendMessageAndSucceed()

	accessLogProducerMock := mocks.NewAsyncProducer(t, nil)
	accessLogProducerMock.ExpectInputAndSucceed()

	// Now, use dependency injection to use the mocks.
	s := &Server{
		DataCollector:     dataCollectorMock,
		AccessLogProducer: accessLogProducerMock,
	}

	// The Server's Close call is important; it will call Close on
	// the two mock producers, which will then validate whether all
	// expectations are resolved.
	defer safeClose(t, s)

	req, err := http.NewRequest("GET", "http://example.com/?data", nil)
	if err != nil {
		t.Fatal(err)
	}
	res := httptest.NewRecorder()
	s.Handler().ServeHTTP(res, req)

	if res.Code != 200 {
		t.Errorf("Expected HTTP status 200, found %d", res.Code)
	}

	if string(res.Body.Bytes()) != "Your data is stored with unique identifier important/0/1" {
		t.Error("Unexpected response body", res.Body)
	}
}
예제 #2
0
func TestClose(t *testing.T) {
	p := Producer{}

	sync_producer := mocks.NewSyncProducer(t, nil)
	p.producer = sync_producer

	p.Close()
	sync_producer.ExpectSendMessageAndSucceed()

	msg := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("message")}
	_, _, err := p.producer.SendMessage(msg)

	if err != nil {
		t.Errorf("extected SyncProducer to be closed, but no error after sending message")
	}
}
예제 #3
0
func TestSendMessage(t *testing.T) {
	var result bool
	error := errors.New("fail")

	sync_producer := mocks.NewSyncProducer(t, nil)
	p := Producer{producer: sync_producer}

	sync_producer.ExpectSendMessageAndSucceed()
	result = p.SendMessage("test", "test message")
	if !result {
		t.Errorf("expected message to be sent, SendMessage() returned %v", result)
	}

	sync_producer.ExpectSendMessageAndFail(error)
	result = p.SendMessage("test", "test message")
	if result {
		t.Errorf("expected message not to be sent, SendMessage() returned %v", result)
	}
}
예제 #4
0
// Create a new producer whose next "Send" on ChainPartition gives you blob #offset.
func mockNewProducer(t *testing.T, cp ChainPartition, offset int64, disk chan *ab.KafkaMessage) Producer {
	mp := &mockProducerImpl{
		producer:       mocks.NewSyncProducer(t, nil),
		checker:        nil,
		disk:           disk,
		producedOffset: 0,
		isSetup:        make(chan struct{}),
		t:              t,
	}
	mp.init(cp, offset)

	if mp.producedOffset == offset-1 {
		close(mp.isSetup)
	} else {
		mp.t.Fatal("Mock producer failed to initialize itself properly")
	}

	return mp
}
// In normal operation, we expect one access log entry,
// and one data collector entry. Let's assume both will succeed.
// We should return a HTTP 200 status.
func TestCollectSuccessfully(t *testing.T) {
	dataCollectorMock := mocks.NewSyncProducer(t, nil)
	dataCollectorMock.ExpectSendMessageAndSucceed()

	// Now, use dependency injection to use the mocks.
	s := &Server{
		DataCollector: dataCollectorMock,
	}

	// The Server's Close call is important; it will call Close on
	// the two mock producers, which will then validate whether all
	// expectations are resolved.
	defer safeClose(t, s)

	var kmsg KafkaMsg = KafkaMsg{
		Topic: "TestTopic",
		Key:   "TestKey",
		Value: "my test value",
	}

	b, err := json.Marshal(kmsg)
	if err != nil {
		fmt.Println("Could not marshal json")
		return
	}

	req, err := http.NewRequest("POST", "http://localhost/", bytes.NewBuffer(b))
	//req, err := http.NewRequest("GET", "http://example.com/?data", nil)
	if err != nil {
		t.Fatal(err)
	}
	res := httptest.NewRecorder()
	s.Handler().ServeHTTP(res, req)

	if res.Code != 200 {
		t.Errorf("Expected HTTP status 200, found %d", res.Code)
	}

	if string(res.Body.Bytes()) != "Your data is stored with unique identifier important/0/1" {
		t.Error("Unexpected response body", res.Body)
	}
}
// We don't expect any data collector calls because the path is wrong,
// so we are not setting any expectations on the dataCollectorMock. It
// will still generate an access log entry though.
func TestWrongPath(t *testing.T) {
	dataCollectorMock := mocks.NewSyncProducer(t, nil)

	s := &Server{
		DataCollector: dataCollectorMock,
	}
	defer safeClose(t, s)

	req, err := http.NewRequest("GET", "http://example.com/wrong?data", nil)
	if err != nil {
		t.Fatal(err)
	}
	res := httptest.NewRecorder()

	s.Handler().ServeHTTP(res, req)

	if res.Code != 404 {
		t.Errorf("Expected HTTP status 404, found %d", res.Code)
	}
}
예제 #7
0
// Now, let's see if we handle the case of not being able to produce
// to the data collector properly. In this case we should return a 500 status.
func TestCollectionFailure(t *testing.T) {
	dataCollectorMock := mocks.NewSyncProducer(t, nil)
	dataCollectorMock.ExpectSendMessageAndFail(sarama.ErrRequestTimedOut)

	accessLogProducerMock := mocks.NewAsyncProducer(t, nil)
	accessLogProducerMock.ExpectInputAndSucceed()

	s := &Server{
		DataCollector:     dataCollectorMock,
		AccessLogProducer: accessLogProducerMock,
	}
	defer safeClose(t, s)

	req, err := http.NewRequest("GET", "http://example.com/?data", nil)
	if err != nil {
		t.Fatal(err)
	}
	res := httptest.NewRecorder()
	s.Handler().ServeHTTP(res, req)

	if res.Code != 500 {
		t.Errorf("Expected HTTP status 500, found %d", res.Code)
	}
}