Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package devices |
| 18 | |
| 19 | import ( |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 20 | "context" |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 21 | "errors" |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame^] | 22 | "testing" |
| 23 | "time" |
| 24 | |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/v2/go/openolt" |
| 26 | "google.golang.org/grpc" |
| 27 | "gotest.tools/assert" |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | type mockStream struct { |
| 31 | grpc.ServerStream |
| 32 | CallCount int |
| 33 | Calls map[int]*openolt.OnuDiscIndication |
| 34 | channel chan int |
| 35 | fail bool |
| 36 | } |
| 37 | |
| 38 | func (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 |
| 49 | func 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 Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 57 | ctx, cancel := context.WithCancel(context.TODO()) |
| 58 | go onu.ProcessOnuMessages(ctx, stream, nil) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 59 | onu.InternalState.SetState("initialized") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame^] | 60 | _ = onu.InternalState.Event("discover") |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 61 | |
| 62 | select { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame^] | 63 | default: |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 64 | 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 Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 69 | cancel() |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // test that if the discovery indication is not acknowledge we'll keep sending new ones |
| 73 | func 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 Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 81 | ctx, cancel := context.WithCancel(context.TODO()) |
| 82 | go onu.ProcessOnuMessages(ctx, stream, nil) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 83 | onu.InternalState.SetState("initialized") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame^] | 84 | _ = onu.InternalState.Event("discover") |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 85 | |
| 86 | select { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame^] | 87 | default: |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 88 | case <-time.After(400 * time.Millisecond): |
| 89 | assert.Equal(t, stream.CallCount, 4) |
| 90 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 91 | cancel() |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // test that if the discovery indication is not acknowledge we'll send a new one |
| 95 | func 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 Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 104 | ctx, cancel := context.WithCancel(context.TODO()) |
| 105 | go onu.ProcessOnuMessages(ctx, stream, nil) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 106 | onu.InternalState.SetState("initialized") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame^] | 107 | _ = onu.InternalState.Event("discover") |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 108 | |
| 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 Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame^] | 118 | default: |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 119 | case <-time.After(1 * time.Second): |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 120 | assert.Equal(t, stream.CallCount, 2) |
| 121 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 122 | cancel() |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 123 | } |