blob: 749791a3eefef9770f3fa4139db7bcf0ebfe8abd [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001/*
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05002 * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
khenaidoo59ce9dd2019-11-11 13:05:32 -05003 *
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
khenaidoob6238b32020-04-07 12:07:36 -040017package kafka
khenaidoo59ce9dd2019-11-11 13:05:32 -050018
19import (
Neha Sharma94f16a92020-06-26 04:17:55 +000020 "context"
Neha Sharma94f16a92020-06-26 04:17:55 +000021 "testing"
22 "time"
khenaidoo26721882021-08-11 17:42:52 -040023
24 "github.com/opencord/voltha-lib-go/v7/pkg/kafka"
khenaidooa5feb8e2021-10-19 17:29:22 -040025 "github.com/opencord/voltha-protos/v5/go/core_adapter"
khenaidoo26721882021-08-11 17:42:52 -040026 "github.com/stretchr/testify/assert"
khenaidoo59ce9dd2019-11-11 13:05:32 -050027)
28
khenaidoo59ce9dd2019-11-11 13:05:32 -050029func TestKafkaClientCreateTopic(t *testing.T) {
Neha Sharma94f16a92020-06-26 04:17:55 +000030 ctx := context.Background()
khenaidoo59ce9dd2019-11-11 13:05:32 -050031 cTkc := NewKafkaClient()
32 topic := kafka.Topic{Name: "myTopic"}
Neha Sharma94f16a92020-06-26 04:17:55 +000033 err := cTkc.CreateTopic(ctx, &topic, 1, 1)
khenaidoo59ce9dd2019-11-11 13:05:32 -050034 assert.Nil(t, err)
Neha Sharma94f16a92020-06-26 04:17:55 +000035 err = cTkc.CreateTopic(ctx, &topic, 1, 1)
khenaidoo59ce9dd2019-11-11 13:05:32 -050036 assert.NotNil(t, err)
37}
38
39func TestKafkaClientDeleteTopic(t *testing.T) {
40 cTkc := NewKafkaClient()
41 topic := kafka.Topic{Name: "myTopic"}
Neha Sharma94f16a92020-06-26 04:17:55 +000042 err := cTkc.DeleteTopic(context.Background(), &topic)
khenaidoo59ce9dd2019-11-11 13:05:32 -050043 assert.Nil(t, err)
44}
45
46func TestKafkaClientSubscribeSend(t *testing.T) {
47 cTkc := NewKafkaClient()
48 topic := kafka.Topic{Name: "myTopic"}
Neha Sharma94f16a92020-06-26 04:17:55 +000049 ch, err := cTkc.Subscribe(context.Background(), &topic)
khenaidoo59ce9dd2019-11-11 13:05:32 -050050 assert.Nil(t, err)
51 assert.NotNil(t, ch)
52 testCh := make(chan bool)
53 maxWait := 5 * time.Millisecond
khenaidooa5feb8e2021-10-19 17:29:22 -040054 msg := &core_adapter.DeviceReason{
khenaidoo26721882021-08-11 17:42:52 -040055 DeviceId: "1234",
56 Reason: "mock",
khenaidoo59ce9dd2019-11-11 13:05:32 -050057 }
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 Sharma94f16a92020-06-26 04:17:55 +000070 err = cTkc.Send(context.Background(), msg, &topic)
khenaidoo59ce9dd2019-11-11 13:05:32 -050071 assert.Nil(t, err)
72 res := <-testCh
73 assert.True(t, res)
74}
75
76func TestKafkaClientUnSubscribe(t *testing.T) {
77 cTkc := NewKafkaClient()
78 topic := kafka.Topic{Name: "myTopic"}
Neha Sharma94f16a92020-06-26 04:17:55 +000079 ch, err := cTkc.Subscribe(context.Background(), &topic)
khenaidoo59ce9dd2019-11-11 13:05:32 -050080 assert.Nil(t, err)
81 assert.NotNil(t, ch)
Neha Sharma94f16a92020-06-26 04:17:55 +000082 err = cTkc.UnSubscribe(context.Background(), &topic, ch)
khenaidoo59ce9dd2019-11-11 13:05:32 -050083 assert.Nil(t, err)
84}
85
86func TestKafkaClientStop(t *testing.T) {
87 cTkc := NewKafkaClient()
88 topic := kafka.Topic{Name: "myTopic"}
Neha Sharma94f16a92020-06-26 04:17:55 +000089 ch, err := cTkc.Subscribe(context.Background(), &topic)
khenaidoo59ce9dd2019-11-11 13:05:32 -050090 assert.Nil(t, err)
91 assert.NotNil(t, ch)
Neha Sharma94f16a92020-06-26 04:17:55 +000092 err = cTkc.UnSubscribe(context.Background(), &topic, ch)
khenaidoo59ce9dd2019-11-11 13:05:32 -050093 assert.Nil(t, err)
Neha Sharma94f16a92020-06-26 04:17:55 +000094 cTkc.Stop(context.Background())
khenaidoo59ce9dd2019-11-11 13:05:32 -050095}