khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | /* |
Joey Armstrong | 9cdee9f | 2024-01-03 04:56:14 -0500 | [diff] [blame] | 2 | * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 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" |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 21 | "testing" |
| 22 | "time" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 23 | |
| 24 | "github.com/opencord/voltha-lib-go/v7/pkg/kafka" |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/v5/go/core_adapter" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 26 | "github.com/stretchr/testify/assert" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 27 | ) |
| 28 | |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 29 | func TestKafkaClientCreateTopic(t *testing.T) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 30 | ctx := context.Background() |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 31 | cTkc := NewKafkaClient() |
| 32 | topic := kafka.Topic{Name: "myTopic"} |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 33 | err := cTkc.CreateTopic(ctx, &topic, 1, 1) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 34 | assert.Nil(t, err) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 35 | err = cTkc.CreateTopic(ctx, &topic, 1, 1) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 36 | assert.NotNil(t, err) |
| 37 | } |
| 38 | |
| 39 | func TestKafkaClientDeleteTopic(t *testing.T) { |
| 40 | cTkc := NewKafkaClient() |
| 41 | topic := kafka.Topic{Name: "myTopic"} |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 42 | err := cTkc.DeleteTopic(context.Background(), &topic) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 43 | assert.Nil(t, err) |
| 44 | } |
| 45 | |
| 46 | func TestKafkaClientSubscribeSend(t *testing.T) { |
| 47 | cTkc := NewKafkaClient() |
| 48 | topic := kafka.Topic{Name: "myTopic"} |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 49 | ch, err := cTkc.Subscribe(context.Background(), &topic) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 50 | assert.Nil(t, err) |
| 51 | assert.NotNil(t, ch) |
| 52 | testCh := make(chan bool) |
| 53 | maxWait := 5 * time.Millisecond |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 54 | msg := &core_adapter.DeviceReason{ |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 55 | DeviceId: "1234", |
| 56 | Reason: "mock", |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 57 | } |
| 58 | timer := time.NewTimer(maxWait) |
| 59 | defer timer.Stop() |
| 60 | go func() { |
| 61 | select { |
| 62 | case val, ok := <-ch: |
| 63 | assert.True(t, ok) |
| 64 | assert.Equal(t, val, msg) |
| 65 | testCh <- true |
| 66 | case <-timer.C: |
| 67 | testCh <- false |
| 68 | } |
| 69 | }() |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 70 | err = cTkc.Send(context.Background(), msg, &topic) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 71 | assert.Nil(t, err) |
| 72 | res := <-testCh |
| 73 | assert.True(t, res) |
| 74 | } |
| 75 | |
| 76 | func TestKafkaClientUnSubscribe(t *testing.T) { |
| 77 | cTkc := NewKafkaClient() |
| 78 | topic := kafka.Topic{Name: "myTopic"} |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 79 | ch, err := cTkc.Subscribe(context.Background(), &topic) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 80 | assert.Nil(t, err) |
| 81 | assert.NotNil(t, ch) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 82 | err = cTkc.UnSubscribe(context.Background(), &topic, ch) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 83 | assert.Nil(t, err) |
| 84 | } |
| 85 | |
| 86 | func TestKafkaClientStop(t *testing.T) { |
| 87 | cTkc := NewKafkaClient() |
| 88 | topic := kafka.Topic{Name: "myTopic"} |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 89 | ch, err := cTkc.Subscribe(context.Background(), &topic) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 90 | assert.Nil(t, err) |
| 91 | assert.NotNil(t, ch) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 92 | err = cTkc.UnSubscribe(context.Background(), &topic, ch) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 93 | assert.Nil(t, err) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 94 | cTkc.Stop(context.Background()) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 95 | } |