forked from peer-calls/peer-calls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
109 lines (85 loc) · 2.23 KB
/
main_test.go
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
package main
import (
"context"
"fmt"
"net"
"net/http"
"os"
"strconv"
"testing"
"time"
"github.com/peer-calls/peer-calls/v4/server/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStartMissingConfig(t *testing.T) {
prefix := "PEERCALLS_"
defer test.UnsetEnvPrefix(prefix)
os.Setenv(prefix+"BIND_PORT", "0")
os.Setenv(prefix+"LOG", "-*")
log := test.NewLogger()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err := start(ctx, log, []string{"-c", "/missing/file.yml"})
require.Error(t, err)
fmt.Printf("error %+v", err)
assert.Contains(t, err.Error(), "read config")
}
func TestStartWrongPort(t *testing.T) {
prefix := "PEERCALLS_"
defer test.UnsetEnvPrefix(prefix)
os.Setenv(prefix+"BIND_PORT", "100000")
os.Setenv(prefix+"LOG", "-*")
log := test.NewLogger()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err := start(ctx, log, []string{})
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid port")
}
func TestStart(t *testing.T) {
prefix := "PEERCALLS_"
defer test.UnsetEnvPrefix(prefix)
l, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 0,
})
require.NoError(t, err, "listener")
port := l.Addr().(*net.TCPAddr).Port
l.Close()
// os.Setenv(prefix+"BIND_ADDR", "127.0.0.1")
os.Setenv(prefix+"BIND_PORT", strconv.Itoa(port))
os.Setenv(prefix+"LOG", "-*")
log := test.NewLogger()
timeoutCtx, cancelTimeout := context.WithTimeout(context.Background(), 1*time.Second)
ctx, cancel := context.WithCancel(timeoutCtx)
defer cancelTimeout()
defer cancel()
errCh := make(chan error, 1)
go func() {
defer close(errCh)
err := start(ctx, log, []string{})
errCh <- err
}()
var r *http.Response
// Keep trying until the server finally starts.
for i := 0; i < 30; i++ {
r, err = http.Get("http://" + net.JoinHostPort("127.0.0.1", strconv.Itoa(port)))
if err != nil {
time.Sleep(20 * time.Millisecond)
continue
}
r.Body.Close()
break
}
if assert.NoError(t, err) {
assert.Equal(t, 200, r.StatusCode)
}
cancel()
select {
case err := <-errCh:
assert.NoError(t, err)
case <-timeoutCtx.Done():
require.Fail(t, "timed out")
}
}