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