blob: d12c5a6da1c39378b09a91ba3292d7d6acafc994 [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 (
20 "errors"
21 "github.com/opencord/voltha-protos/v2/go/openolt"
22 "google.golang.org/grpc"
23 "gotest.tools/assert"
24 "testing"
25 "time"
26)
27
28type mockStream struct {
29 grpc.ServerStream
30 CallCount int
31 Calls map[int]*openolt.OnuDiscIndication
32 channel chan int
33 fail bool
34}
35
36func (s *mockStream) Send(ind *openolt.Indication) error {
37 s.CallCount++
38 if s.fail {
39 return errors.New("fake-error")
40 }
41 s.Calls[s.CallCount] = ind.GetOnuDiscInd()
42 s.channel <- s.CallCount
43 return nil
44}
45
46// test that we're sending a Discovery indication to VOLTHA
47func Test_Onu_DiscoverIndication_send_on_discovery(t *testing.T) {
48 onu := createTestOnu()
49 stream := &mockStream{
50 CallCount: 0,
51 Calls: make(map[int]*openolt.OnuDiscIndication),
52 fail: false,
53 channel: make(chan int, 10),
54 }
55 go onu.ProcessOnuMessages(stream, nil)
56 onu.InternalState.SetState("initialized")
57 onu.InternalState.Event("discover")
58
59 select {
60 case <-time.After(90 * time.Millisecond):
61 assert.Equal(t, stream.CallCount, 1)
62 assert.Equal(t, stream.Calls[1].IntfId, onu.PonPortID)
63 assert.Equal(t, stream.Calls[1].SerialNumber, onu.SerialNumber)
64 }
65}
66
67// test that if the discovery indication is not acknowledge we'll keep sending new ones
68func Test_Onu_DiscoverIndication_retry_on_discovery(t *testing.T) {
69 onu := createTestOnu()
70 stream := &mockStream{
71 CallCount: 0,
72 Calls: make(map[int]*openolt.OnuDiscIndication),
73 fail: false,
74 channel: make(chan int, 10),
75 }
76 go onu.ProcessOnuMessages(stream, nil)
77 onu.InternalState.SetState("initialized")
78 onu.InternalState.Event("discover")
79
80 select {
81 case <-time.After(400 * time.Millisecond):
82 assert.Equal(t, stream.CallCount, 4)
83 }
84}
85
86// test that if the discovery indication is not acknowledge we'll send a new one
87func Test_Onu_DiscoverIndication_retry_on_discovery_stops(t *testing.T) {
88 onu := createTestOnu()
89 onu.DiscoveryRetryDelay = 500 * time.Millisecond
90 stream := &mockStream{
91 CallCount: 0,
92 Calls: make(map[int]*openolt.OnuDiscIndication),
93 fail: false,
94 channel: make(chan int, 10),
95 }
96 go onu.ProcessOnuMessages(stream, nil)
97 onu.InternalState.SetState("initialized")
98 onu.InternalState.Event("discover")
99
100 go func() {
101 for calls := range stream.channel {
102 if calls == 2 {
103 onu.InternalState.SetState("enabled")
104 }
105 }
106 }()
107
108 select {
109 case <-time.After(1 * time.Second):
110
111 assert.Equal(t, stream.CallCount, 2)
112 }
113}