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