diff options
author | MikoĊaj Walczak <mikiwalczak+github@gmail.com> | 2018-07-12 10:51:38 +0100 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-07-12 10:51:38 +0100 |
commit | 8e3bcdab237624421034ccc4eb16f260d4338aec (patch) | |
tree | 0cb93b736c59506f68df67ac1150e80047dc202c /dhcpv6/future_test.go | |
parent | 34154e71da6f5b4527809dc0babdefcbd262281c (diff) |
Asynchronous client for DHCPv6 (#80)
Diffstat (limited to 'dhcpv6/future_test.go')
-rw-r--r-- | dhcpv6/future_test.go | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/dhcpv6/future_test.go b/dhcpv6/future_test.go new file mode 100644 index 0000000..bee87e3 --- /dev/null +++ b/dhcpv6/future_test.go @@ -0,0 +1,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) +} |