Ejemplo n.º 1
0
func TestMockClientBadFetch(t *testing.T) {
	client := NewMockClient(0, 100)
	client.fetchResponseError = siesta.ErrBrokerNotAvailable
	response, err := client.Fetch("test", 0, 0)
	assert.Equal(t, nil, err)
	assert.Len(t, response.Data, 1)
	assert.Len(t, response.Data["test"], 1)
	assert.Equal(t, siesta.ErrBrokerNotAvailable, response.Data["test"][0].Error)
}
Ejemplo n.º 2
0
func TestMockClientGoodFetch(t *testing.T) {
	client := NewMockClient(0, 200)
	response, err := client.Fetch("test", 0, 0)
	assert.Equal(t, nil, err)
	assert.Len(t, response.Data, 1)
	assert.Len(t, response.Data["test"], 1)
	assert.Equal(t, siesta.ErrNoError, response.Data["test"][0].Error)
	assert.Len(t, response.Data["test"][0].Messages, int(client.fetchSize))
	assert.Equal(t, client.highwaterMarkOffset, response.Data["test"][0].HighwaterMarkOffset)

	for i := 0; i < 100; i++ {
		assert.Equal(t, int64(i), response.Data["test"][0].Messages[i].Offset)
		assert.Equal(t, fmt.Sprintf("message-%d", i), string(response.Data["test"][0].Messages[i].Message.Value))
	}
}
Ejemplo n.º 3
0
func TestTCPConsumer(t *testing.T) {
	consumer := NewMockConsumer()

	tcpConsumer := NewConsumer(addr, consumer)
	err := tcpConsumer.Start()
	assert.Equal(t, nil, err)

	client, err := NewClient(addr)
	assert.Equal(t, nil, err)

	// add
	// good case
	err = client.Add("asd", 1)
	assert.Equal(t, nil, err)

	// add existing
	err = client.Add("asd", 1)
	assert.Regexp(t, regexp.MustCompile(".*already exists.*"), err.Error())

	// remove
	// good case
	err = client.Remove("asd", 1)
	assert.Equal(t, nil, err)

	// remove non-existing
	err = client.Remove("asd", 1)
	assert.Regexp(t, regexp.MustCompile(".*does not exist.*"), err.Error())

	// assignments
	// empty assignments
	assignments, err := client.Assignment()
	assert.Equal(t, nil, err)
	assert.Empty(t, assignments)

	// add one
	err = client.Add("asd", 1)
	assert.Equal(t, nil, err)

	// check now
	assignments, err = client.Assignment()
	assert.Equal(t, nil, err)
	assert.Len(t, assignments, 1)

	// remove that one and check again
	err = client.Remove("asd", 1)
	assert.Equal(t, nil, err)

	assignments, err = client.Assignment()
	assert.Equal(t, nil, err)
	assert.Empty(t, assignments)

	// commit
	// commit something
	err = client.Commit("asd", 1, 123)
	assert.Equal(t, nil, err)

	// commit error
	consumer.commitOffsetError = errors.New("boom")
	err = client.Commit("asd", 1, 123)
	assert.Regexp(t, regexp.MustCompile(".*boom.*"), err)
	consumer.commitOffsetError = nil

	// get offset
	// non-existing
	_, err = client.Offset("asd", 1)
	assert.Regexp(t, regexp.MustCompile(".*does not exist.*"), err.Error())

	// add back
	err = client.Add("asd", 1)
	assert.Equal(t, nil, err)

	// check again
	offset, err := client.Offset("asd", 1)
	assert.Equal(t, nil, err)
	assert.Equal(t, int64(123), offset)

	// commit offset
	err = client.Commit("asd", 1, 234)
	assert.Equal(t, nil, err)

	// check once more
	offset, err = client.Offset("asd", 1)
	assert.Equal(t, nil, err)
	assert.Equal(t, int64(234), offset)

	// set offset
	// remove previous
	err = client.Remove("asd", 1)
	assert.Equal(t, nil, err)

	// non-existing
	err = client.SetOffset("asd", 1, 123)
	assert.Regexp(t, regexp.MustCompile(".*does not exist.*"), err.Error())

	// add first
	err = client.Add("asd", 1)
	assert.Equal(t, nil, err)

	// check again
	err = client.SetOffset("asd", 1, 123)
	assert.Equal(t, nil, err)

	// get offset
	offset, err = client.Offset("asd", 1)
	assert.Equal(t, nil, err)
	assert.Equal(t, int64(123), offset)

	// lag
	// remove previous
	err = client.Remove("asd", 1)
	assert.Equal(t, nil, err)

	// non-existing
	_, err = client.Lag("asd", 1)
	assert.Regexp(t, regexp.MustCompile(".*does not exist.*"), err.Error())

	// add first
	err = client.Add("asd", 1)
	assert.Equal(t, nil, err)

	// check again
	lag, err := client.Lag("asd", 1)
	assert.Equal(t, nil, err)
	assert.Equal(t, int64(100), lag)

	// custom handler
	tcpConsumer.RegisterCustomHandler("custom", func(body []byte) (*Response, error) {
		return NewResponse(true, "", "hello!"), nil
	})

	response, err := client.CustomRequest("custom", nil)
	assert.Equal(t, nil, err)
	var responseValue string
	err = json.Unmarshal(response, &responseValue)
	assert.Equal(t, nil, err)
	assert.Equal(t, "hello!", responseValue)

	// unknown key
	_, err = client.CustomRequest("foobar", nil)
	assert.Regexp(t, regexp.MustCompile("Unknown request key.*"), err.Error())

	// await termination
	success := make(chan struct{})
	go func() {
		tcpConsumer.AwaitTermination()
		success <- struct{}{}
	}()

	select {
	case <-success:
		t.Fatal("Await termination shouldn't unblock until consumer is not stopped")
	case <-time.After(timeout):
	}

	tcpConsumer.Stop()
	select {
	case <-success:
	case <-time.After(timeout):
		t.Fatalf("Await termination failed to unblock within %s", timeout)
	}
}