Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package kafka |
| 17 | |
| 18 | import ( |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 19 | "context" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 20 | "github.com/stretchr/testify/assert" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | func TestDefaultKafkaProxy(t *testing.T) { |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 25 | actualResult := newInterContainerProxy() |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 26 | assert.Equal(t, actualResult.kafkaAddress, DefaultKafkaAddress) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 27 | assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil)) |
| 28 | } |
| 29 | |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 30 | func 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 Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 33 | assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil)) |
| 34 | } |
| 35 | |
| 36 | func TestKafkaProxyOptionTopic(t *testing.T) { |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 37 | actualResult := newInterContainerProxy(DefaultTopic(&Topic{Name: "Adapter"})) |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 38 | assert.Equal(t, actualResult.kafkaAddress, DefaultKafkaAddress) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 39 | assert.Equal(t, actualResult.defaultRequestHandlerInterface, interface{}(nil)) |
Matteo Scandolo | f346a2d | 2020-01-24 13:14:54 -0800 | [diff] [blame] | 40 | assert.Equal(t, actualResult.defaultTopic.Name, "Adapter") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | type myInterface struct { |
| 44 | } |
| 45 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 46 | func TestKafkaProxyOptionTargetInterface(t *testing.T) { |
| 47 | var m *myInterface |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 48 | actualResult := newInterContainerProxy(RequestHandlerInterface(m)) |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 49 | assert.Equal(t, actualResult.kafkaAddress, DefaultKafkaAddress) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 50 | assert.Equal(t, actualResult.defaultRequestHandlerInterface, m) |
| 51 | } |
| 52 | |
| 53 | func TestKafkaProxyChangeAllOptions(t *testing.T) { |
| 54 | var m *myInterface |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 55 | actualResult := newInterContainerProxy( |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 56 | InterContainerAddress("10.20.30.40:1020"), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 57 | DefaultTopic(&Topic{Name: "Adapter"}), |
| 58 | RequestHandlerInterface(m)) |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 59 | assert.Equal(t, actualResult.kafkaAddress, "10.20.30.40:1020") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 60 | assert.Equal(t, actualResult.defaultRequestHandlerInterface, m) |
Matteo Scandolo | f346a2d | 2020-01-24 13:14:54 -0800 | [diff] [blame] | 61 | assert.Equal(t, actualResult.defaultTopic.Name, "Adapter") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 62 | } |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 63 | |
| 64 | func TestKafkaProxyEnableLivenessChannel(t *testing.T) { |
| 65 | var m *myInterface |
| 66 | |
| 67 | // Note: This doesn't actually start the client |
| 68 | client := NewSaramaClient() |
| 69 | |
Kent Hagerman | 3a40230 | 2020-01-31 15:03:53 -0500 | [diff] [blame] | 70 | probe := newInterContainerProxy( |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 71 | InterContainerAddress("10.20.30.40:1020"), |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 72 | DefaultTopic(&Topic{Name: "Adapter"}), |
| 73 | RequestHandlerInterface(m), |
| 74 | MsgClient(client), |
| 75 | ) |
| 76 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 77 | ch := probe.EnableLivenessChannel(context.Background(), true) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 78 | |
| 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 | } |