blob: 56c90ca1d02150adda1ef6128f629702f7a778de [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 (
19 "github.com/stretchr/testify/assert"
20 "testing"
21)
22
23func TestDefaultKafkaProxy(t *testing.T) {
Kent Hagerman3a402302020-01-31 15:03:53 -050024 actualResult := newInterContainerProxy()
Scott Baker2c1c4822019-10-16 11:02:41 -070025 assert.Equal(t, actualResult.kafkaHost, DefaultKafkaHost)
26 assert.Equal(t, actualResult.kafkaPort, DefaultKafkaPort)
27 assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil))
28}
29
30func TestKafkaProxyOptionHost(t *testing.T) {
Kent Hagerman3a402302020-01-31 15:03:53 -050031 actualResult := newInterContainerProxy(InterContainerHost("10.20.30.40"))
Scott Baker2c1c4822019-10-16 11:02:41 -070032 assert.Equal(t, actualResult.kafkaHost, "10.20.30.40")
33 assert.Equal(t, actualResult.kafkaPort, DefaultKafkaPort)
34 assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil))
35}
36
37func TestKafkaProxyOptionPort(t *testing.T) {
Kent Hagerman3a402302020-01-31 15:03:53 -050038 actualResult := newInterContainerProxy(InterContainerPort(1020))
Scott Baker2c1c4822019-10-16 11:02:41 -070039 assert.Equal(t, actualResult.kafkaHost, DefaultKafkaHost)
40 assert.Equal(t, actualResult.kafkaPort, 1020)
41 assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil))
42}
43
44func TestKafkaProxyOptionTopic(t *testing.T) {
Kent Hagerman3a402302020-01-31 15:03:53 -050045 actualResult := newInterContainerProxy(DefaultTopic(&Topic{Name: "Adapter"}))
Scott Baker2c1c4822019-10-16 11:02:41 -070046 assert.Equal(t, actualResult.kafkaHost, DefaultKafkaHost)
47 assert.Equal(t, actualResult.kafkaPort, DefaultKafkaPort)
48 assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil))
Matteo Scandolof346a2d2020-01-24 13:14:54 -080049 assert.Equal(t, actualResult.defaultTopic.Name, "Adapter")
Scott Baker2c1c4822019-10-16 11:02:41 -070050}
51
52type myInterface struct {
53}
54
55func (m *myInterface) doSomething() {
56}
57
58func TestKafkaProxyOptionTargetInterface(t *testing.T) {
59 var m *myInterface
Kent Hagerman3a402302020-01-31 15:03:53 -050060 actualResult := newInterContainerProxy(RequestHandlerInterface(m))
Scott Baker2c1c4822019-10-16 11:02:41 -070061 assert.Equal(t, actualResult.kafkaHost, DefaultKafkaHost)
62 assert.Equal(t, actualResult.kafkaPort, DefaultKafkaPort)
63 assert.Equal(t, actualResult.defaultRequestHandlerInterface, m)
64}
65
66func TestKafkaProxyChangeAllOptions(t *testing.T) {
67 var m *myInterface
Kent Hagerman3a402302020-01-31 15:03:53 -050068 actualResult := newInterContainerProxy(
Scott Baker2c1c4822019-10-16 11:02:41 -070069 InterContainerHost("10.20.30.40"),
70 InterContainerPort(1020),
71 DefaultTopic(&Topic{Name: "Adapter"}),
72 RequestHandlerInterface(m))
Scott Baker2c1c4822019-10-16 11:02:41 -070073 assert.Equal(t, actualResult.kafkaHost, "10.20.30.40")
74 assert.Equal(t, actualResult.kafkaPort, 1020)
75 assert.Equal(t, actualResult.defaultRequestHandlerInterface, m)
Matteo Scandolof346a2d2020-01-24 13:14:54 -080076 assert.Equal(t, actualResult.defaultTopic.Name, "Adapter")
Scott Baker2c1c4822019-10-16 11:02:41 -070077}
Scott Baker104b67d2019-10-29 15:56:27 -070078
79func TestKafkaProxyEnableLivenessChannel(t *testing.T) {
80 var m *myInterface
81
82 // Note: This doesn't actually start the client
83 client := NewSaramaClient()
84
Kent Hagerman3a402302020-01-31 15:03:53 -050085 probe := newInterContainerProxy(
Scott Baker104b67d2019-10-29 15:56:27 -070086 InterContainerHost("10.20.30.40"),
87 InterContainerPort(1020),
88 DefaultTopic(&Topic{Name: "Adapter"}),
89 RequestHandlerInterface(m),
90 MsgClient(client),
91 )
92
Scott Baker104b67d2019-10-29 15:56:27 -070093 ch := probe.EnableLivenessChannel(true)
94
95 // The channel should have one "true" message on it
96 assert.NotEmpty(t, ch)
97
98 select {
99 case stuff := <-ch:
100 assert.True(t, stuff)
101 default:
102 t.Error("Failed to read from the channel")
103 }
104}