github.com/uber/kraken@v0.1.4/lib/torrent/scheduler/conn/handshaker_test.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package conn
    15  
    16  import (
    17  	"net"
    18  	"sync"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/uber/kraken/core"
    25  	"github.com/uber/kraken/lib/torrent/storage"
    26  	"github.com/uber/kraken/utils/bitsetutil"
    27  )
    28  
    29  func TestHandshakerSetsConnFieldsProperly(t *testing.T) {
    30  	require := require.New(t)
    31  
    32  	config := ConfigFixture()
    33  
    34  	h1 := HandshakerFixture(config)
    35  	l1, err := net.Listen("tcp", "localhost:0")
    36  	require.NoError(err)
    37  	defer l1.Close()
    38  
    39  	namespace := core.TagFixture()
    40  	h2 := HandshakerFixture(config)
    41  
    42  	info := storage.TorrentInfoFixture(4, 1)
    43  	emptyRemoteBitfields := make(RemoteBitfields)
    44  	remoteBitfields := RemoteBitfields{
    45  		core.PeerIDFixture(): bitsetutil.FromBools(true, false),
    46  		core.PeerIDFixture(): bitsetutil.FromBools(false, true),
    47  	}
    48  
    49  	var wg sync.WaitGroup
    50  
    51  	start := time.Now()
    52  
    53  	wg.Add(1)
    54  	go func() {
    55  		defer wg.Done()
    56  
    57  		nc, err := l1.Accept()
    58  		require.NoError(err)
    59  
    60  		pc, err := h1.Accept(nc)
    61  		require.NoError(err)
    62  		require.Equal(h2.peerID, pc.PeerID())
    63  		require.Equal(info.Digest(), pc.Digest())
    64  		require.Equal(info.InfoHash(), pc.InfoHash())
    65  		require.Equal(info.Bitfield(), pc.Bitfield())
    66  		require.Equal(namespace, pc.Namespace())
    67  
    68  		c, err := h1.Establish(pc, info, remoteBitfields)
    69  		require.NoError(err)
    70  		require.Equal(h2.peerID, c.PeerID())
    71  		require.Equal(info.InfoHash(), c.InfoHash())
    72  		require.True(c.CreatedAt().After(start))
    73  	}()
    74  
    75  	wg.Add(1)
    76  	go func() {
    77  		defer wg.Done()
    78  
    79  		r, err := h2.Initialize(h1.peerID, l1.Addr().String(), info, emptyRemoteBitfields, namespace)
    80  		require.NoError(err)
    81  		require.Equal(h1.peerID, r.Conn.PeerID())
    82  		require.Equal(info.InfoHash(), r.Conn.InfoHash())
    83  		require.True(r.Conn.CreatedAt().After(start))
    84  		require.Equal(info.Bitfield(), r.Bitfield)
    85  		require.Equal(remoteBitfields, r.RemoteBitfields)
    86  	}()
    87  
    88  	wg.Wait()
    89  }