File: peerdiscovery_test.go

package info (click to toggle)
golang-github-schollz-peerdiscovery 1.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 152 kB
  • sloc: makefile: 10
file content (60 lines) | stat: -rw-r--r-- 1,400 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
54
55
56
57
58
59
60
package peerdiscovery

import (
	"fmt"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestDiscovery(t *testing.T) {
	for _, version := range []IPVersion{IPv4, IPv6} {
		// should not be able to "discover" itself
		discoveries, err := Discover(Settings{
			TimeLimit: 5 * time.Second,
			Delay:     500 * time.Millisecond,
		})
		assert.Nil(t, err)
		assert.Zero(t, len(discoveries))

		// should be able to "discover" itself
		discoveries, err = Discover(Settings{
			Limit:     -1,
			AllowSelf: true,
			Payload:   []byte("payload"),
			Delay:     500 * time.Millisecond,
			TimeLimit: 1 * time.Second,
			IPVersion: version,
		})
		fmt.Println(discoveries)
		assert.Nil(t, err)
		assert.NotZero(t, len(discoveries))
	}
}

func TestDiscoverySelf(t *testing.T) {
	for _, version := range []IPVersion{IPv4, IPv6} {
		// broadcast self to self
		go func() {
			_, err := Discover(Settings{
				Limit:     -1,
				Payload:   []byte("payload"),
				Delay:     10 * time.Millisecond,
				TimeLimit: 2 * time.Second,
				IPVersion: version,
			})
			assert.Nil(t, err)
		}()
		discoveries, err := Discover(Settings{
			Limit:            1,
			Payload:          []byte("payload"),
			Delay:            500 * time.Millisecond,
			TimeLimit:        2 * time.Second,
			DisableBroadcast: true,
			AllowSelf:        true,
		})
		assert.Nil(t, err)
		assert.NotZero(t, len(discoveries))
	}
}