blob: 54674135b479aeefe0432f8a59f961ae579a7294 [file] [log] [blame]
Matteo Scandolo4b077aa2021-02-16 17:33:37 -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 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
131 const (
132 allocId1 = 1024
133 allocId2 = 1025
134 )
135
136 pon := &PonPort{
137 AllocatedAllocIds: make(map[uint16]*openolt.SerialNumber),
138 }
139
140 pon.AllocatedAllocIds[allocId1] = sn1
141 pon.AllocatedAllocIds[allocId2] = sn2
142
143 assert.Equal(t, len(pon.AllocatedAllocIds), 2)
144
145 pon.removeAllocId(sn1)
146
147 assert.Equal(t, len(pon.AllocatedAllocIds), 1)
148 assert.Nil(t, pon.AllocatedAllocIds[allocId1])
149 assert.Equal(t, pon.AllocatedAllocIds[allocId2], sn2)
150
151}