1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package stun
import (
"errors"
"testing"
"time"
)
func TestAgent_ProcessInTransaction(t *testing.T) {
m := New()
a := NewAgent(func(e Event) {
if e.Error != nil {
t.Errorf("got error: %s", e.Error)
}
if !e.Message.Equal(m) {
t.Errorf("%s (got) != %s (expected)", e.Message, m)
}
})
if err := m.NewTransactionID(); err != nil {
t.Fatal(err)
}
if err := a.Start(m.TransactionID, time.Time{}); err != nil {
t.Fatal(err)
}
if err := a.Process(m); err != nil {
t.Error(err)
}
if err := a.Close(); err != nil {
t.Error(err)
}
}
func TestAgent_Process(t *testing.T) {
m := New()
a := NewAgent(func(e Event) {
if e.Error != nil {
t.Errorf("got error: %s", e.Error)
}
if !e.Message.Equal(m) {
t.Errorf("%s (got) != %s (expected)", e.Message, m)
}
})
if err := m.NewTransactionID(); err != nil {
t.Fatal(err)
}
if err := a.Process(m); err != nil {
t.Error(err)
}
if err := a.Close(); err != nil {
t.Error(err)
}
if err := a.Process(m); !errors.Is(err, ErrAgentClosed) {
t.Errorf("closed agent should return <%s>, but got <%s>",
ErrAgentClosed, err,
)
}
}
func TestAgent_Start(t *testing.T) {
a := NewAgent(nil)
id := NewTransactionID()
deadline := time.Now().AddDate(0, 0, 1)
if err := a.Start(id, deadline); err != nil {
t.Errorf("failed to statt transaction: %s", err)
}
if err := a.Start(id, deadline); !errors.Is(err, ErrTransactionExists) {
t.Errorf("duplicate start should return <%s>, got <%s>",
ErrTransactionExists, err,
)
}
if err := a.Close(); err != nil {
t.Error(err)
}
id = NewTransactionID()
if err := a.Start(id, deadline); !errors.Is(err, ErrAgentClosed) {
t.Errorf("start on closed agent should return <%s>, got <%s>",
ErrAgentClosed, err,
)
}
if err := a.SetHandler(nil); !errors.Is(err, ErrAgentClosed) {
t.Errorf("SetHandler on closed agent should return <%s>, got <%s>",
ErrAgentClosed, err,
)
}
}
func TestAgent_Stop(t *testing.T) {
called := make(chan Event, 1)
a := NewAgent(func(e Event) {
called <- e
})
if err := a.Stop(transactionID{}); !errors.Is(err, ErrTransactionNotExists) {
t.Fatalf("unexpected error: %s, should be %s", err, ErrTransactionNotExists)
}
id := NewTransactionID()
timeout := time.Millisecond * 200
if err := a.Start(id, time.Now().Add(timeout)); err != nil {
t.Fatal(err)
}
if err := a.Stop(id); err != nil {
t.Fatal(err)
}
select {
case e := <-called:
if !errors.Is(e.Error, ErrTransactionStopped) {
t.Fatalf("unexpected error: %s, should be %s",
e.Error, ErrTransactionStopped,
)
}
case <-time.After(timeout * 2):
t.Fatal("timed out")
}
if err := a.Close(); err != nil {
t.Fatal(err)
}
if err := a.Close(); !errors.Is(err, ErrAgentClosed) {
t.Fatalf("a.Close returned %s instead of %s", err, ErrAgentClosed)
}
if err := a.Stop(transactionID{}); !errors.Is(err, ErrAgentClosed) {
t.Fatalf("unexpected error: %s, should be %s", err, ErrAgentClosed)
}
}
func TestAgent_GC(t *testing.T) {
a := NewAgent(nil)
shouldTimeOutID := make(map[transactionID]bool)
deadline := time.Date(2027, time.November, 21,
23, 0, 0, 0,
time.UTC,
)
gcDeadline := deadline.Add(-time.Second)
deadlineNotGC := gcDeadline.AddDate(0, 0, -1)
a.SetHandler(func(e Event) { //nolint:errcheck,gosec
id := e.TransactionID
shouldTimeOut, found := shouldTimeOutID[id]
if !found {
t.Error("unexpected transaction ID")
}
if shouldTimeOut && !errors.Is(e.Error, ErrTransactionTimeOut) {
t.Errorf("%x should time out, but got %v", id, e.Error)
}
if !shouldTimeOut && errors.Is(e.Error, ErrTransactionTimeOut) {
t.Errorf("%x should not time out, but got %v", id, e.Error)
}
})
for i := 0; i < 5; i++ {
id := NewTransactionID()
shouldTimeOutID[id] = false
if err := a.Start(id, deadline); err != nil {
t.Fatal(err)
}
}
for i := 0; i < 5; i++ {
id := NewTransactionID()
shouldTimeOutID[id] = true
if err := a.Start(id, deadlineNotGC); err != nil {
t.Fatal(err)
}
}
if err := a.Collect(gcDeadline); err != nil {
t.Fatal(err)
}
if err := a.Close(); err != nil {
t.Error(err)
}
if err := a.Collect(gcDeadline); !errors.Is(err, ErrAgentClosed) {
t.Errorf("should <%s>, but got <%s>", ErrAgentClosed, err)
}
}
func BenchmarkAgent_GC(b *testing.B) {
a := NewAgent(nil)
deadline := time.Now().AddDate(0, 0, 1)
for i := 0; i < agentCollectCap; i++ {
if err := a.Start(NewTransactionID(), deadline); err != nil {
b.Fatal(err)
}
}
defer func() {
if err := a.Close(); err != nil {
b.Error(err)
}
}()
b.ReportAllocs()
gcDeadline := deadline.Add(-time.Second)
for i := 0; i < b.N; i++ {
if err := a.Collect(gcDeadline); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkAgent_Process(b *testing.B) {
a := NewAgent(nil)
deadline := time.Now().AddDate(0, 0, 1)
for i := 0; i < 1000; i++ {
if err := a.Start(NewTransactionID(), deadline); err != nil {
b.Fatal(err)
}
}
defer func() {
if err := a.Close(); err != nil {
b.Error(err)
}
}()
b.ReportAllocs()
m := MustBuild(TransactionID)
for i := 0; i < b.N; i++ {
if err := a.Process(m); err != nil {
b.Fatal(err)
}
}
}
|