| 1 |
/* |
| 2 |
* Copyright (c) 2024 Contributors to the Eclipse Foundation |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 6 |
* and Eclipse Distribution License v1.0 which accompany this distribution. |
| 7 |
* |
| 8 |
* The Eclipse Public License is available at |
| 9 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 10 |
* and the Eclipse Distribution License is available at |
| 11 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 12 |
* |
| 13 |
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 14 |
*/ |
| 15 |
|
| 16 |
package state |
| 17 |
|
| 18 |
import ( |
| 19 |
"math" |
| 20 |
"math/rand" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/eclipse/paho.golang/packets" |
| 25 |
"github.com/eclipse/paho.golang/paho/session" |
| 26 |
"github.com/stretchr/testify/assert" |
| 27 |
) |
| 28 |
|
| 29 |
// TestPacketIdAllocateAndFreeAll checks that we can allocate all packet identifiers and that, when freed, a message is always |
| 30 |
// sent to the response channel |
| 31 |
func TestPacketIdAllocateAndFreeAll(t *testing.T) { |
| 32 |
ss := NewInMemory() |
| 33 |
ss.clientPackets = make(map[uint16]clientGenerated) |
| 34 |
ss.inflight = newSendQuota(200) // not testing this but its needed for endClientGenerated to work |
| 35 |
|
| 36 |
// Use full band |
| 37 |
cpChan := make(chan packets.ControlPacket) |
| 38 |
for i := uint16(1); i != 0; i++ { |
| 39 |
v, _ := ss.allocateNextPacketId(packets.PUBLISH, cpChan) |
| 40 |
assert.Equal(t, i, v) |
| 41 |
} |
| 42 |
|
| 43 |
// Trying to allocate another ID should fail |
| 44 |
_, err := ss.allocateNextPacketId(packets.PUBLISH, cpChan) |
| 45 |
assert.ErrorIs(t, err, session.ErrPacketIdentifiersExhausted) |
| 46 |
|
| 47 |
// Free all Mids |
| 48 |
allResponded := make(chan struct{}) |
| 49 |
go func() { |
| 50 |
for i := uint16(0); i < midMax; i++ { |
| 51 |
<-cpChan |
| 52 |
} |
| 53 |
close(allResponded) |
| 54 |
}() |
| 55 |
|
| 56 |
resp := packets.ControlPacket{ |
| 57 |
Content: nil, |
| 58 |
FixedHeader: packets.FixedHeader{ |
| 59 |
Type: packets.PUBACK, |
| 60 |
Flags: 0, |
| 61 |
}, |
| 62 |
} |
| 63 |
for i := uint16(1); i != 0; i++ { |
| 64 |
assert.NoError(t, ss.endClientGenerated(i, &resp)) |
| 65 |
} |
| 66 |
select { |
| 67 |
case <-allResponded: |
| 68 |
case <-time.After(time.Second): |
| 69 |
t.Fatal("did not receive responses") |
| 70 |
} |
| 71 |
select { |
| 72 |
case <-cpChan: |
| 73 |
t.Fatal("unexpected response") |
| 74 |
default: |
| 75 |
} |
| 76 |
|
| 77 |
// Allocate all Mids again |
| 78 |
for i := uint16(1); i != 0; i++ { |
| 79 |
v, _ := ss.allocateNextPacketId(packets.PUBLISH, cpChan) |
| 80 |
assert.Equal(t, i, v) |
| 81 |
} |
| 82 |
|
| 83 |
// Closing the store should free all Ids sending a message to the provided channel |
| 84 |
gotCp := make(chan struct{}) |
| 85 |
go func() { |
| 86 |
for i := uint16(0); i < midMax; i++ { |
| 87 |
<-cpChan |
| 88 |
} |
| 89 |
close(gotCp) |
| 90 |
}() |
| 91 |
ss.Close() |
| 92 |
select { |
| 93 |
case <-gotCp: |
| 94 |
case <-time.After(time.Second): |
| 95 |
t.Fatal("did not receive responses") |
| 96 |
} |
| 97 |
select { |
| 98 |
case <-cpChan: |
| 99 |
t.Fatal("unexpected response") |
| 100 |
default: |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// TestPacketIdHoles confirms that random "holes" within the packet ID map will be found and utilised |
| 105 |
func TestPacketIdHoles(t *testing.T) { |
| 106 |
ss := NewInMemory() |
| 107 |
ss.clientPackets = make(map[uint16]clientGenerated) |
| 108 |
ss.inflight = newSendQuota(200) // not testing this but its needed for endClientGenerated to work |
| 109 |
|
| 110 |
// For this test we ignore responses |
| 111 |
cpChan := make(chan packets.ControlPacket) |
| 112 |
defer close(cpChan) |
| 113 |
go func() { |
| 114 |
for range cpChan { |
| 115 |
} |
| 116 |
}() |
| 117 |
|
| 118 |
// Allocate all Mids |
| 119 |
for i := uint16(1); i != 0; i++ { |
| 120 |
v, _ := ss.allocateNextPacketId(packets.PUBLISH, cpChan) |
| 121 |
assert.Equal(t, i, v) |
| 122 |
} |
| 123 |
|
| 124 |
resp := packets.ControlPacket{ |
| 125 |
Content: nil, |
| 126 |
FixedHeader: packets.FixedHeader{ |
| 127 |
Type: packets.PUBACK, |
| 128 |
Flags: 0, |
| 129 |
}, |
| 130 |
} |
| 131 |
|
| 132 |
// Currently MIDs.index is filled in, randomly dig some holes and try to fill in all of them again. |
| 133 |
h := map[uint16]bool{} |
| 134 |
for i := 0; i < 60000; i++ { |
| 135 |
r := uint16(rand.Intn(math.MaxUint16)) |
| 136 |
r += 1 // Want 0-65535 |
| 137 |
|
| 138 |
ss.endClientGenerated(r, &resp) |
| 139 |
h[r] = true |
| 140 |
} |
| 141 |
t.Log("Num of holes:", len(h)) |
| 142 |
for i := 0; i < len(h); i++ { |
| 143 |
_, err := ss.allocateNextPacketId(packets.PUBLISH, cpChan) |
| 144 |
assert.Nil(t, err) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
// Expecting TestPAcketIdsFreeZeroID.Free(0) always do nothing (no panic), because 0 identifier is invalid and ignored. |
| 149 |
func TestPAcketIdsFreeZeroID(t *testing.T) { |
| 150 |
ss := NewInMemory() |
| 151 |
|
| 152 |
resp := packets.ControlPacket{ |
| 153 |
Content: nil, |
| 154 |
FixedHeader: packets.FixedHeader{ |
| 155 |
Type: packets.PUBACK, |
| 156 |
Flags: 0, |
| 157 |
}, |
| 158 |
} |
| 159 |
assert.NotPanics(t, func() { assert.NoError(t, ss.endClientGenerated(0, &resp)) }) |
| 160 |
} |