github.com/braveheart12/insolar-09-08-19@v0.8.7/network/transport/future_test.go (about)

     1  /*
     2   * The Clear BSD License
     3   *
     4   * Copyright (c) 2019 Insolar Technologies
     5   *
     6   * All rights reserved.
     7   *
     8   * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
     9   *
    10   *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11   *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12   *  Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13   *
    14   * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15   *
    16   */
    17  
    18  package transport
    19  
    20  import (
    21  	"sync"
    22  	"sync/atomic"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/insolar/insolar/network"
    27  	"github.com/insolar/insolar/network/transport/host"
    28  	"github.com/insolar/insolar/network/transport/packet"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestNewFuture(t *testing.T) {
    33  	n, _ := host.NewHost("127.0.0.1:8080")
    34  	cb := func(f Future) {}
    35  	m := &packet.Packet{}
    36  	f := NewFuture(network.RequestID(1), n, m, cb)
    37  
    38  	require.Implements(t, (*Future)(nil), f)
    39  }
    40  
    41  func TestFuture_ID(t *testing.T) {
    42  	n, _ := host.NewHost("127.0.0.1:8080")
    43  	cb := func(f Future) {}
    44  	m := &packet.Packet{}
    45  	f := NewFuture(network.RequestID(1), n, m, cb)
    46  
    47  	require.Equal(t, f.ID(), network.RequestID(1))
    48  }
    49  
    50  func TestFuture_Actor(t *testing.T) {
    51  	n, _ := host.NewHost("127.0.0.1:8080")
    52  	cb := func(f Future) {}
    53  	m := &packet.Packet{}
    54  	f := NewFuture(network.RequestID(1), n, m, cb)
    55  
    56  	require.Equal(t, f.Actor(), n)
    57  }
    58  
    59  func TestFuture_Result(t *testing.T) {
    60  	n, _ := host.NewHost("127.0.0.1:8080")
    61  	cb := func(f Future) {}
    62  	m := &packet.Packet{}
    63  	f := NewFuture(network.RequestID(1), n, m, cb)
    64  
    65  	require.Empty(t, f.Result())
    66  }
    67  
    68  func TestFuture_Request(t *testing.T) {
    69  	n, _ := host.NewHost("127.0.0.1:8080")
    70  	cb := func(f Future) {}
    71  	m := &packet.Packet{}
    72  	f := NewFuture(network.RequestID(1), n, m, cb)
    73  
    74  	require.Equal(t, f.Request(), m)
    75  }
    76  
    77  func TestFuture_SetResult(t *testing.T) {
    78  	n, _ := host.NewHost("127.0.0.1:8080")
    79  	cb := func(f Future) {}
    80  	m := &packet.Packet{}
    81  	f := NewFuture(network.RequestID(1), n, m, cb)
    82  
    83  	require.Empty(t, f.Result())
    84  
    85  	go f.SetResult(m)
    86  
    87  	m2 := <-f.Result() // Result() call closes channel
    88  
    89  	require.Equal(t, m, m2)
    90  
    91  	m3, err := f.GetResult(10 * time.Millisecond)
    92  	// legal behavior, the channel is closed because of the previous f.Result() call finished the Future
    93  	require.EqualError(t, err, "channel closed")
    94  	require.Nil(t, m3)
    95  }
    96  
    97  func TestFuture_Cancel(t *testing.T) {
    98  	n, _ := host.NewHost("127.0.0.1:8080")
    99  
   100  	cbCalled := false
   101  
   102  	cb := func(f Future) { cbCalled = true }
   103  
   104  	m := &packet.Packet{}
   105  	f := NewFuture(network.RequestID(1), n, m, cb)
   106  
   107  	f.Cancel()
   108  
   109  	_, closed := <-f.Result()
   110  
   111  	require.False(t, closed)
   112  	require.True(t, cbCalled)
   113  }
   114  
   115  func TestFuture_GetResult(t *testing.T) {
   116  	n, _ := host.NewHost("127.0.0.1:8080")
   117  	m := &packet.Packet{}
   118  	var cancelled uint32 = 0
   119  	cancelCallback := func(f Future) {
   120  		atomic.StoreUint32(&cancelled, 1)
   121  	}
   122  	f := NewFuture(network.RequestID(1), n, m, cancelCallback)
   123  	go func() {
   124  		time.Sleep(time.Millisecond)
   125  		f.Cancel()
   126  	}()
   127  
   128  	_, err := f.GetResult(10 * time.Millisecond)
   129  	require.Error(t, err)
   130  	require.Equal(t, uint32(1), atomic.LoadUint32(&cancelled))
   131  }
   132  
   133  func TestFuture_GetResult2(t *testing.T) {
   134  	n, _ := host.NewHost("127.0.0.1:8080")
   135  	c := make(chan *packet.Packet)
   136  	var f Future = &future{
   137  		result:         c,
   138  		actor:          n,
   139  		request:        &packet.Packet{},
   140  		requestID:      network.RequestID(1),
   141  		cancelCallback: func(f Future) {},
   142  	}
   143  	go func() {
   144  		time.Sleep(time.Millisecond)
   145  		close(c)
   146  	}()
   147  	_, err := f.GetResult(10 * time.Millisecond)
   148  	require.Error(t, err)
   149  }
   150  
   151  func TestFuture_SetResult_Cancel_Concurrency(t *testing.T) {
   152  	n, _ := host.NewHost("127.0.0.1:8080")
   153  
   154  	cbCalled := false
   155  
   156  	cb := func(f Future) { cbCalled = true }
   157  
   158  	m := &packet.Packet{}
   159  	f := NewFuture(network.RequestID(1), n, m, cb)
   160  
   161  	wg := &sync.WaitGroup{}
   162  	wg.Add(2)
   163  
   164  	go func() {
   165  		f.Cancel()
   166  		wg.Done()
   167  	}()
   168  	go func() {
   169  		f.SetResult(&packet.Packet{})
   170  		wg.Done()
   171  	}()
   172  
   173  	wg.Wait()
   174  	res, ok := <-f.Result()
   175  
   176  	cancelDone := res == nil && !ok
   177  	resultDone := res != nil && ok
   178  
   179  	require.True(t, cancelDone || resultDone)
   180  	require.True(t, cbCalled)
   181  }