File: channel_bind_test.go

package info (click to toggle)
golang-github-pion-turn.v2 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, forky, sid, trixie
  • size: 716 kB
  • sloc: makefile: 4
file content (53 lines) | stat: -rw-r--r-- 1,145 bytes parent folder | download
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
package allocation

import (
	"net"
	"testing"
	"time"

	"github.com/pion/turn/v2/internal/proto"
)

func TestChannelBind(t *testing.T) {
	c := newChannelBind(2 * time.Second)

	if c.allocation.GetChannelByNumber(c.Number) != c {
		t.Errorf("GetChannelByNumber(%d) shouldn't be nil after added to allocation", c.Number)
	}
}

func TestChannelBindStart(t *testing.T) {
	c := newChannelBind(2 * time.Second)

	time.Sleep(3 * time.Second)

	if c.allocation.GetChannelByNumber(c.Number) != nil {
		t.Errorf("GetChannelByNumber(%d) should be nil if timeout", c.Number)
	}
}

func TestChannelBindReset(t *testing.T) {
	c := newChannelBind(3 * time.Second)

	time.Sleep(2 * time.Second)
	c.refresh(3 * time.Second)
	time.Sleep(2 * time.Second)

	if c.allocation.GetChannelByNumber(c.Number) == nil {
		t.Errorf("GetChannelByNumber(%d) shouldn't be nil after refresh", c.Number)
	}
}

func newChannelBind(lifetime time.Duration) *ChannelBind {
	a := NewAllocation(nil, nil, nil)

	addr, _ := net.ResolveUDPAddr("udp", "0.0.0.0:0")
	c := &ChannelBind{
		Number: proto.MinChannelNumber,
		Peer:   addr,
	}

	_ = a.AddChannelBind(c, lifetime)

	return c
}