blob: 433b776068235a69eb3c737189114533f9a6db0a [file] [log] [blame]
Matteo Scandoloe811ae92019-12-10 17:50:14 -08001/*
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
17package devices
18
19import (
David Bainbridge103cf022019-12-16 20:11:35 +000020 "context"
Matteo Scandoloe811ae92019-12-10 17:50:14 -080021 "errors"
Shrey Baid688b4242020-07-10 20:40:10 +053022 "testing"
23 "time"
24
Matteo Scandoloe811ae92019-12-10 17:50:14 -080025 "github.com/opencord/voltha-protos/v2/go/openolt"
26 "google.golang.org/grpc"
27 "gotest.tools/assert"
Matteo Scandoloe811ae92019-12-10 17:50:14 -080028)
29
30type mockStream struct {
31 grpc.ServerStream
32 CallCount int
33 Calls map[int]*openolt.OnuDiscIndication
34 channel chan int
35 fail bool
36}
37
38func (s *mockStream) Send(ind *openolt.Indication) error {
39 s.CallCount++
40 if s.fail {
41 return errors.New("fake-error")
42 }
43 s.Calls[s.CallCount] = ind.GetOnuDiscInd()
44 s.channel <- s.CallCount
45 return nil
46}
47
48// test that we're sending a Discovery indication to VOLTHA
49func Test_Onu_DiscoverIndication_send_on_discovery(t *testing.T) {
50 onu := createTestOnu()
51 stream := &mockStream{
52 CallCount: 0,
53 Calls: make(map[int]*openolt.OnuDiscIndication),
54 fail: false,
55 channel: make(chan int, 10),
56 }
David Bainbridge103cf022019-12-16 20:11:35 +000057 ctx, cancel := context.WithCancel(context.TODO())
58 go onu.ProcessOnuMessages(ctx, stream, nil)
Matteo Scandoloe811ae92019-12-10 17:50:14 -080059 onu.InternalState.SetState("initialized")
Shrey Baid688b4242020-07-10 20:40:10 +053060 _ = onu.InternalState.Event("discover")
Matteo Scandoloe811ae92019-12-10 17:50:14 -080061
62 select {
Shrey Baid688b4242020-07-10 20:40:10 +053063 default:
Matteo Scandoloe811ae92019-12-10 17:50:14 -080064 case <-time.After(90 * time.Millisecond):
65 assert.Equal(t, stream.CallCount, 1)
66 assert.Equal(t, stream.Calls[1].IntfId, onu.PonPortID)
67 assert.Equal(t, stream.Calls[1].SerialNumber, onu.SerialNumber)
68 }
David Bainbridge103cf022019-12-16 20:11:35 +000069 cancel()
Matteo Scandoloe811ae92019-12-10 17:50:14 -080070}
71
72// test that if the discovery indication is not acknowledge we'll keep sending new ones
73func Test_Onu_DiscoverIndication_retry_on_discovery(t *testing.T) {
74 onu := createTestOnu()
75 stream := &mockStream{
76 CallCount: 0,
77 Calls: make(map[int]*openolt.OnuDiscIndication),
78 fail: false,
79 channel: make(chan int, 10),
80 }
David Bainbridge103cf022019-12-16 20:11:35 +000081 ctx, cancel := context.WithCancel(context.TODO())
82 go onu.ProcessOnuMessages(ctx, stream, nil)
Matteo Scandoloe811ae92019-12-10 17:50:14 -080083 onu.InternalState.SetState("initialized")
Shrey Baid688b4242020-07-10 20:40:10 +053084 _ = onu.InternalState.Event("discover")
Matteo Scandoloe811ae92019-12-10 17:50:14 -080085
86 select {
Shrey Baid688b4242020-07-10 20:40:10 +053087 default:
Matteo Scandoloe811ae92019-12-10 17:50:14 -080088 case <-time.After(400 * time.Millisecond):
89 assert.Equal(t, stream.CallCount, 4)
90 }
David Bainbridge103cf022019-12-16 20:11:35 +000091 cancel()
Matteo Scandoloe811ae92019-12-10 17:50:14 -080092}
93
94// test that if the discovery indication is not acknowledge we'll send a new one
95func Test_Onu_DiscoverIndication_retry_on_discovery_stops(t *testing.T) {
96 onu := createTestOnu()
97 onu.DiscoveryRetryDelay = 500 * time.Millisecond
98 stream := &mockStream{
99 CallCount: 0,
100 Calls: make(map[int]*openolt.OnuDiscIndication),
101 fail: false,
102 channel: make(chan int, 10),
103 }
David Bainbridge103cf022019-12-16 20:11:35 +0000104 ctx, cancel := context.WithCancel(context.TODO())
105 go onu.ProcessOnuMessages(ctx, stream, nil)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800106 onu.InternalState.SetState("initialized")
Shrey Baid688b4242020-07-10 20:40:10 +0530107 _ = onu.InternalState.Event("discover")
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800108
109 go func() {
110 for calls := range stream.channel {
111 if calls == 2 {
112 onu.InternalState.SetState("enabled")
113 }
114 }
115 }()
116
117 select {
Shrey Baid688b4242020-07-10 20:40:10 +0530118 default:
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800119 case <-time.After(1 * time.Second):
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800120 assert.Equal(t, stream.CallCount, 2)
121 }
David Bainbridge103cf022019-12-16 20:11:35 +0000122 cancel()
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800123}