Shrey Baid | 64cda47 | 2020-04-24 18:58:18 +0530 | [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 | |
| 17 | package common |
| 18 | |
| 19 | import ( |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/Shopify/sarama" |
| 23 | "gotest.tools/assert" |
| 24 | ) |
| 25 | |
| 26 | type mockAsyncProducer struct { |
| 27 | input chan *sarama.ProducerMessage |
| 28 | successes chan *sarama.ProducerMessage |
| 29 | errors chan *sarama.ProducerError |
| 30 | } |
| 31 | |
| 32 | func (p mockAsyncProducer) AsyncClose() { |
| 33 | |
| 34 | } |
| 35 | |
| 36 | func (p mockAsyncProducer) Close() error { |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | func (p mockAsyncProducer) Input() chan<- *sarama.ProducerMessage { |
| 41 | return p.input |
| 42 | } |
| 43 | |
| 44 | func (p mockAsyncProducer) Successes() <-chan *sarama.ProducerMessage { |
| 45 | return p.successes |
| 46 | } |
| 47 | |
| 48 | func (p mockAsyncProducer) Errors() <-chan *sarama.ProducerError { |
| 49 | return p.errors |
| 50 | } |
| 51 | |
| 52 | type mockSarama struct{} |
| 53 | |
| 54 | func (m mockSarama) NewAsyncProducer(addrs []string, conf *sarama.Config) (sarama.AsyncProducer, error) { |
| 55 | |
| 56 | producer := &mockAsyncProducer{ |
| 57 | errors: make(chan *sarama.ProducerError), |
| 58 | input: make(chan *sarama.ProducerMessage), |
| 59 | successes: make(chan *sarama.ProducerMessage), |
| 60 | } |
| 61 | |
| 62 | return producer, nil |
| 63 | } |
| 64 | |
| 65 | func TestInitializePublisher(t *testing.T) { |
| 66 | mockLib := mockSarama{} |
| 67 | err := InitializePublisher(mockLib.NewAsyncProducer, 0) |
| 68 | |
| 69 | assert.Equal(t, err, nil) |
| 70 | assert.Equal(t, topic, "BBSim-OLT-0-Events") |
| 71 | |
| 72 | Options.BBSim.KafkaEventTopic = "Testing-Topic" |
| 73 | err = InitializePublisher(mockLib.NewAsyncProducer, 0) |
| 74 | assert.Equal(t, topic, "Testing-Topic") |
| 75 | assert.Equal(t, err, nil) |
| 76 | } |