blob: 8c88750d373e4589bed25c2294134e5c943f599e [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package kafka
17
18import (
Neha Sharma94f16a92020-06-26 04:17:55 +000019 "context"
Scott Baker2c1c4822019-10-16 11:02:41 -070020 "github.com/stretchr/testify/assert"
21 "testing"
22)
23
24func TestDefaultKafkaProxy(t *testing.T) {
Kent Hagerman3a402302020-01-31 15:03:53 -050025 actualResult := newInterContainerProxy()
Neha Sharmadd9af392020-04-28 09:03:57 +000026 assert.Equal(t, actualResult.kafkaAddress, DefaultKafkaAddress)
Scott Baker2c1c4822019-10-16 11:02:41 -070027 assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil))
28}
29
Neha Sharmadd9af392020-04-28 09:03:57 +000030func TestKafkaProxyOptionAddress(t *testing.T) {
31 actualResult := newInterContainerProxy(InterContainerAddress("10.20.30.40:1020"))
32 assert.Equal(t, actualResult.kafkaAddress, "10.20.30.40:1020")
Scott Baker2c1c4822019-10-16 11:02:41 -070033 assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil))
34}
35
36func TestKafkaProxyOptionTopic(t *testing.T) {
Kent Hagerman3a402302020-01-31 15:03:53 -050037 actualResult := newInterContainerProxy(DefaultTopic(&Topic{Name: "Adapter"}))
Neha Sharmadd9af392020-04-28 09:03:57 +000038 assert.Equal(t, actualResult.kafkaAddress, DefaultKafkaAddress)
Scott Baker2c1c4822019-10-16 11:02:41 -070039 assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil))
Matteo Scandolof346a2d2020-01-24 13:14:54 -080040 assert.Equal(t, actualResult.defaultTopic.Name, "Adapter")
Scott Baker2c1c4822019-10-16 11:02:41 -070041}
42
43type myInterface struct {
44}
45
Scott Baker2c1c4822019-10-16 11:02:41 -070046func TestKafkaProxyOptionTargetInterface(t *testing.T) {
47 var m *myInterface
Kent Hagerman3a402302020-01-31 15:03:53 -050048 actualResult := newInterContainerProxy(RequestHandlerInterface(m))
Neha Sharmadd9af392020-04-28 09:03:57 +000049 assert.Equal(t, actualResult.kafkaAddress, DefaultKafkaAddress)
Scott Baker2c1c4822019-10-16 11:02:41 -070050 assert.Equal(t, actualResult.defaultRequestHandlerInterface, m)
51}
52
53func TestKafkaProxyChangeAllOptions(t *testing.T) {
54 var m *myInterface
Kent Hagerman3a402302020-01-31 15:03:53 -050055 actualResult := newInterContainerProxy(
Neha Sharmadd9af392020-04-28 09:03:57 +000056 InterContainerAddress("10.20.30.40:1020"),
Scott Baker2c1c4822019-10-16 11:02:41 -070057 DefaultTopic(&Topic{Name: "Adapter"}),
58 RequestHandlerInterface(m))
Neha Sharmadd9af392020-04-28 09:03:57 +000059 assert.Equal(t, actualResult.kafkaAddress, "10.20.30.40:1020")
Scott Baker2c1c4822019-10-16 11:02:41 -070060 assert.Equal(t, actualResult.defaultRequestHandlerInterface, m)
Matteo Scandolof346a2d2020-01-24 13:14:54 -080061 assert.Equal(t, actualResult.defaultTopic.Name, "Adapter")
Scott Baker2c1c4822019-10-16 11:02:41 -070062}
Scott Baker104b67d2019-10-29 15:56:27 -070063
64func TestKafkaProxyEnableLivenessChannel(t *testing.T) {
65 var m *myInterface
66
67 // Note: This doesn't actually start the client
68 client := NewSaramaClient()
69
Kent Hagerman3a402302020-01-31 15:03:53 -050070 probe := newInterContainerProxy(
Neha Sharmadd9af392020-04-28 09:03:57 +000071 InterContainerAddress("10.20.30.40:1020"),
Scott Baker104b67d2019-10-29 15:56:27 -070072 DefaultTopic(&Topic{Name: "Adapter"}),
73 RequestHandlerInterface(m),
74 MsgClient(client),
75 )
76
Neha Sharma94f16a92020-06-26 04:17:55 +000077 ch := probe.EnableLivenessChannel(context.Background(), true)
Scott Baker104b67d2019-10-29 15:56:27 -070078
79 // The channel should have one "true" message on it
80 assert.NotEmpty(t, ch)
81
82 select {
83 case stuff := <-ch:
84 assert.True(t, stuff)
85 default:
86 t.Error("Failed to read from the channel")
87 }
88}