khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 17 | package kafka |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 18 | |
| 19 | import ( |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 20 | "testing" |
| 21 | "time" |
| 22 | |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 24 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 25 | "github.com/stretchr/testify/assert" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 26 | ) |
| 27 | |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 28 | func TestKafkaClientCreateTopic(t *testing.T) { |
| 29 | cTkc := NewKafkaClient() |
| 30 | topic := kafka.Topic{Name: "myTopic"} |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 31 | err := cTkc.CreateTopic(&topic, 1, 1) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 32 | assert.Nil(t, err) |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 33 | err = cTkc.CreateTopic(&topic, 1, 1) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 34 | assert.NotNil(t, err) |
| 35 | } |
| 36 | |
| 37 | func TestKafkaClientDeleteTopic(t *testing.T) { |
| 38 | cTkc := NewKafkaClient() |
| 39 | topic := kafka.Topic{Name: "myTopic"} |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 40 | err := cTkc.DeleteTopic(&topic) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 41 | assert.Nil(t, err) |
| 42 | } |
| 43 | |
| 44 | func TestKafkaClientSubscribeSend(t *testing.T) { |
| 45 | cTkc := NewKafkaClient() |
| 46 | topic := kafka.Topic{Name: "myTopic"} |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 47 | ch, err := cTkc.Subscribe(&topic) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 48 | assert.Nil(t, err) |
| 49 | assert.NotNil(t, ch) |
| 50 | testCh := make(chan bool) |
| 51 | maxWait := 5 * time.Millisecond |
| 52 | msg := &ic.InterContainerMessage{ |
| 53 | Header: &ic.Header{Id: "1234", ToTopic: topic.Name}, |
| 54 | Body: nil, |
| 55 | } |
| 56 | timer := time.NewTimer(maxWait) |
| 57 | defer timer.Stop() |
| 58 | go func() { |
| 59 | select { |
| 60 | case val, ok := <-ch: |
| 61 | assert.True(t, ok) |
| 62 | assert.Equal(t, val, msg) |
| 63 | testCh <- true |
| 64 | case <-timer.C: |
| 65 | testCh <- false |
| 66 | } |
| 67 | }() |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 68 | err = cTkc.Send(msg, &topic) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 69 | assert.Nil(t, err) |
| 70 | res := <-testCh |
| 71 | assert.True(t, res) |
| 72 | } |
| 73 | |
| 74 | func TestKafkaClientUnSubscribe(t *testing.T) { |
| 75 | cTkc := NewKafkaClient() |
| 76 | topic := kafka.Topic{Name: "myTopic"} |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 77 | ch, err := cTkc.Subscribe(&topic) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 78 | assert.Nil(t, err) |
| 79 | assert.NotNil(t, ch) |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 80 | err = cTkc.UnSubscribe(&topic, ch) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 81 | assert.Nil(t, err) |
| 82 | } |
| 83 | |
| 84 | func TestKafkaClientStop(t *testing.T) { |
| 85 | cTkc := NewKafkaClient() |
| 86 | topic := kafka.Topic{Name: "myTopic"} |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 87 | ch, err := cTkc.Subscribe(&topic) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 88 | assert.Nil(t, err) |
| 89 | assert.NotNil(t, ch) |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 90 | err = cTkc.UnSubscribe(&topic, ch) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 91 | assert.Nil(t, err) |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 92 | cTkc.Stop() |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 93 | } |