summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/future_test.go
blob: bee87e388ae67b0ed10e16cb828ca81bf78473d7 (plain)
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
package dhcpv6

import (
	"errors"
	"testing"
	"time"

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

func TestResponseValue(t *testing.T) {
	m, err := NewMessage()
	require.NoError(t, err)
	r := NewResponse(m, nil)
	require.Equal(t, r.Value(), m)
	require.Equal(t, r.Error(), nil)
}

func TestResponseError(t *testing.T) {
	e := errors.New("Test error")
	r := NewResponse(nil, e)
	require.Equal(t, r.Value(), nil)
	require.Equal(t, r.Error(), e)
}

func TestSuccessFuture(t *testing.T) {
	m, err := NewMessage()
	require.NoError(t, err)
	f := NewSuccessFuture(m)

	val, err := f.Wait()
	require.NoError(t, err)
	require.Equal(t, val, m)
}

func TestFailureFuture(t *testing.T) {
	e := errors.New("Test error")
	f := NewFailureFuture(e)

	val, err := f.Wait()
	require.Equal(t, err, e)
	require.Equal(t, val, nil)
}

func TestThenSuccess(t *testing.T) {
	m, err := NewMessage()
	require.NoError(t, err)
	s, err := NewMessage()
	require.NoError(t, err)
	e := errors.New("Test error")

	f := NewSuccessFuture(m).
		Then(func(_ DHCPv6) Future {
			return NewSuccessFuture(s)
		}, func(_ error) Future {
			return NewFailureFuture(e)
		})

	val, err := f.Wait()
	require.NoError(t, err)
	require.NotEqual(t, val, m)
	require.Equal(t, val, s)
}

func TestThenFailure(t *testing.T) {
	m, err := NewMessage()
	require.NoError(t, err)
	s, err := NewMessage()
	require.NoError(t, err)
	e := errors.New("Test error")
	e2 := errors.New("Test error 2")

	f := NewFailureFuture(e).
		Then(func(_ DHCPv6) Future {
			return NewSuccessFuture(s)
		}, func(_ error) Future {
			return NewFailureFuture(e2)
		})

	val, err := f.Wait()
	require.Error(t, err)
	require.NotEqual(t, val, m)
	require.NotEqual(t, val, s)
	require.NotEqual(t, err, e)
	require.Equal(t, err, e2)
}

func TestOnSuccess(t *testing.T) {
	m, err := NewMessage()
	require.NoError(t, err)
	s, err := NewMessage()
	require.NoError(t, err)

	f := NewSuccessFuture(m).
		OnSuccess(func(_ DHCPv6) Future {
			return NewSuccessFuture(s)
		})

	val, err := f.Wait()
	require.NoError(t, err)
	require.NotEqual(t, val, m)
	require.Equal(t, val, s)
}

func TestOnSuccessForFailureFuture(t *testing.T) {
	m, err := NewMessage()
	require.NoError(t, err)
	e := errors.New("Test error")

	f := NewFailureFuture(e).
		OnSuccess(func(_ DHCPv6) Future {
			return NewSuccessFuture(m)
		})

	val, err := f.Wait()
	require.Error(t, err)
	require.Equal(t, err, e)
	require.NotEqual(t, val, m)
}

func TestOnFailure(t *testing.T) {
	m, err := NewMessage()
	require.NoError(t, err)
	s, err := NewMessage()
	require.NoError(t, err)
	e := errors.New("Test error")

	f := NewFailureFuture(e).
		OnFailure(func(_ error) Future {
			return NewSuccessFuture(s)
		})

	val, err := f.Wait()
	require.NoError(t, err)
	require.NotEqual(t, val, m)
	require.Equal(t, val, s)
}

func TestOnFailureForSuccessFuture(t *testing.T) {
	m, err := NewMessage()
	require.NoError(t, err)
	s, err := NewMessage()
	require.NoError(t, err)

	f := NewSuccessFuture(m).
		OnFailure(func(_ error) Future {
			return NewSuccessFuture(s)
		})

	val, err := f.Wait()
	require.NoError(t, err)
	require.NotEqual(t, val, s)
	require.Equal(t, val, m)
}

func TestWaitTimeout(t *testing.T) {
	m, err := NewMessage()
	require.NoError(t, err)
	s, err := NewMessage()
	require.NoError(t, err)
	f := NewSuccessFuture(m).OnSuccess(func(_ DHCPv6) Future {
		time.Sleep(1 * time.Second)
		return NewSuccessFuture(s)
	})
	val, err := f.WaitTimeout(50 * time.Millisecond)
	require.Error(t, err)
	require.Equal(t, err.Error(), "Timed out")
	require.NotEqual(t, val, m)
	require.NotEqual(t, val, s)
}