blob: 86dca2fa02776a040a817cfcfa4c8db9516df883 [file] [log] [blame]
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08003
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 K. Bainbridgec415efe2021-08-19 13:05:21 +000020 "github.com/opencord/voltha-protos/v5/go/openolt"
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080021 "github.com/stretchr/testify/assert"
22 "sync"
23 "testing"
24)
25
26var sn1 = NewSN(0, 0, 1)
27var sn2 = NewSN(0, 0, 2)
28var sn3 = NewSN(0, 0, 3)
29
30// NOTE that we are using a benchmark test to actually test concurrency
31func Benchmark_storeGemPort(b *testing.B) {
32 pon := PonPort{
33 AllocatedGemPorts: make(map[uint16]*openolt.SerialNumber),
34 }
35
36 wg := sync.WaitGroup{}
37 wg.Add(3)
38
39 // concurrently add multiple ports
40 go func(wg *sync.WaitGroup) { pon.storeGemPort(1, sn1); wg.Done() }(&wg)
41 go func(wg *sync.WaitGroup) { pon.storeGemPort(2, sn2); wg.Done() }(&wg)
42 go func(wg *sync.WaitGroup) { pon.storeGemPort(3, sn3); wg.Done() }(&wg)
43
44 wg.Wait()
45
46 assert.Equal(b, len(pon.AllocatedGemPorts), 3)
47}
48
49func Benchmark_removeGemPort(b *testing.B) {
50 pon := PonPort{
51 AllocatedGemPorts: make(map[uint16]*openolt.SerialNumber),
52 }
53
54 pon.storeGemPort(1, sn1)
55 pon.storeGemPort(2, sn2)
56 pon.storeGemPort(3, sn3)
57
58 assert.Equal(b, len(pon.AllocatedGemPorts), 3)
59
60 wg := sync.WaitGroup{}
61 wg.Add(3)
62
63 // concurrently add multiple ports
64 go func(wg *sync.WaitGroup) { pon.removeGemPort(1); wg.Done() }(&wg)
65 go func(wg *sync.WaitGroup) { pon.removeGemPort(2); wg.Done() }(&wg)
66 go func(wg *sync.WaitGroup) { pon.removeGemPort(3); wg.Done() }(&wg)
67
68 wg.Wait()
69
70 assert.Equal(b, len(pon.AllocatedGemPorts), 0)
71}
72
73func Test_removeGemPort(t *testing.T) {
74 pon := &PonPort{
75 AllocatedGemPorts: make(map[uint16]*openolt.SerialNumber),
76 }
77
78 pon.storeGemPort(1, sn1)
79 pon.storeGemPort(2, sn2)
80 assert.Equal(t, len(pon.AllocatedGemPorts), 2)
81
82 // remove a non exiting gemPort
83 pon.removeGemPort(3)
84 assert.Equal(t, len(pon.AllocatedGemPorts), 2)
85
86 // remove an existing gemPort
87 pon.removeGemPort(1)
88 assert.Equal(t, len(pon.AllocatedGemPorts), 1)
89
90}
91
92func Test_removeGemPortBySn(t *testing.T) {
93 pon := &PonPort{
94 AllocatedGemPorts: make(map[uint16]*openolt.SerialNumber),
95 }
96
97 pon.storeGemPort(1, sn1)
98 pon.storeGemPort(2, sn2)
99 assert.Equal(t, len(pon.AllocatedGemPorts), 2)
100
101 // remove a non exiting gemPort
102 pon.removeGemPortBySn(sn1)
103 assert.Equal(t, len(pon.AllocatedGemPorts), 1)
104 assert.Nil(t, pon.AllocatedGemPorts[1])
105 assert.Equal(t, pon.AllocatedGemPorts[2], sn2)
106}
107
108func Test_isGemPortAllocated(t *testing.T) {
109 pon := &PonPort{
110 AllocatedGemPorts: make(map[uint16]*openolt.SerialNumber),
111 }
112
113 pon.storeGemPort(1, sn1)
114
115 assert.Equal(t, len(pon.AllocatedGemPorts), 1)
116
117 free, sn := pon.isGemPortAllocated(1)
118
119 assert.Equal(t, free, true)
120 assert.Equal(t, sn, sn1)
121
122 used, sn_ := pon.isGemPortAllocated(2)
123
124 assert.Equal(t, used, false)
125 assert.Nil(t, sn_)
126}
127
128// the allocId is never removed, is always set to either 255 or 65535
129func Test_removeAllocId(t *testing.T) {
130
Girish Gowdra574834a2022-02-04 15:15:15 -0800131 const entityID1 uint16 = 1024
132 const entityID2 uint16 = 1025
133 const allocId1 uint16 = 1024
134 const allocId2 uint16 = 1025
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800135
136 pon := &PonPort{
Girish Gowdra574834a2022-02-04 15:15:15 -0800137 AllocatedAllocIds: make(map[AllocIDKey]*AllocIDVal),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800138 }
139
Girish Gowdra574834a2022-02-04 15:15:15 -0800140 pon.AllocatedAllocIds[AllocIDKey{0, 1, entityID1}] = &AllocIDVal{sn1, allocId1}
141 pon.AllocatedAllocIds[AllocIDKey{1, 1, entityID2}] = &AllocIDVal{sn2, allocId2}
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800142
143 assert.Equal(t, len(pon.AllocatedAllocIds), 2)
144
Girish Gowdra574834a2022-02-04 15:15:15 -0800145 pon.removeAllocId(0, 1, entityID1)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800146
147 assert.Equal(t, len(pon.AllocatedAllocIds), 1)
Girish Gowdra574834a2022-02-04 15:15:15 -0800148 assert.NotContains(t, pon.AllocatedAllocIds, AllocIDKey{0, 1, entityID1})
149 assert.Contains(t, pon.AllocatedAllocIds, AllocIDKey{1, 1, entityID2})
150 assert.Equal(t, pon.AllocatedAllocIds[AllocIDKey{1, 1, entityID2}].OnuSn, sn2)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800151}