コード例 #1
0
ファイル: utp_test.go プロジェクト: musha68k/go-ipfs
func TestUTPPingPong(t *testing.T) {
	defer goroutineLeakCheck(t)()
	s, err := NewSocket("udp", "localhost:0")
	require.NoError(t, err)
	defer s.Close()
	pingerClosed := make(chan struct{})
	go func() {
		defer close(pingerClosed)
		b, err := Dial(s.Addr().String())
		require.NoError(t, err)
		defer b.Close()
		n, err := b.Write([]byte("ping"))
		require.NoError(t, err)
		require.EqualValues(t, 4, n)
		buf := make([]byte, 4)
		b.Read(buf)
		require.EqualValues(t, "pong", buf)
		log.Printf("got pong")
	}()
	a, err := s.Accept()
	require.NoError(t, err)
	defer a.Close()
	log.Printf("accepted %s", a)
	buf := make([]byte, 42)
	n, err := a.Read(buf)
	require.NoError(t, err)
	require.EqualValues(t, "ping", buf[:n])
	log.Print("got ping")
	n, err = a.Write([]byte("pong"))
	require.NoError(t, err)
	require.Equal(t, 4, n)
	log.Print("waiting for pinger to close")
	<-pingerClosed
}
コード例 #2
0
ファイル: utp_test.go プロジェクト: musha68k/go-ipfs
func TestConnReadDeadline(t *testing.T) {
	ls, _ := NewSocket("udp", "localhost:0")
	ds, _ := NewSocket("udp", "localhost:0")
	dcReadErr := make(chan error)
	go func() {
		c, _ := ds.Dial(ls.Addr().String())
		defer c.Close()
		_, err := c.Read(nil)
		dcReadErr <- err
	}()
	c, _ := ls.Accept()
	dl := time.Now().Add(time.Millisecond)
	c.SetReadDeadline(dl)
	_, err := c.Read(nil)
	require.Equal(t, errTimeout, err)
	// The deadline has passed.
	if !time.Now().After(dl) {
		t.FailNow()
	}
	// Returns timeout on subsequent read.
	_, err = c.Read(nil)
	require.Equal(t, errTimeout, err)
	// Disable the deadline.
	c.SetReadDeadline(time.Time{})
	readReturned := make(chan struct{})
	go func() {
		c.Read(nil)
		close(readReturned)
	}()
	select {
	case <-readReturned:
		// Read returned but shouldn't have.
		t.FailNow()
	case <-time.After(time.Millisecond):
	}
	c.Close()
	select {
	case <-readReturned:
	case <-time.After(time.Millisecond):
		t.Fatal("read should return after Conn is closed")
	}
	if err := <-dcReadErr; err != io.EOF {
		t.Fatalf("dial conn read returned %s", err)
	}
}
コード例 #3
0
ファイル: iterator_test.go プロジェクト: musha68k/go-ipfs
func TestIterator(t *testing.T) {
	const s = "AAAABBBCCDAABBB"
	si := StringIterator(s)
	for i := range s {
		require.True(t, si.Next())
		require.Equal(t, s[i], si.Value().(byte))
	}
	require.False(t, si.Next())
}
コード例 #4
0
ファイル: utp_test.go プロジェクト: musha68k/go-ipfs
// Check that peer sending FIN doesn't cause unread data to be dropped in a
// receiver.
func TestReadFinishedConn(t *testing.T) {
	a, b := connPair()
	defer a.Close()
	defer b.Close()
	mu.Lock()
	originalAPDC := artificialPacketDropChance
	artificialPacketDropChance = 1
	mu.Unlock()
	n, err := a.Write([]byte("hello"))
	require.Equal(t, 5, n)
	require.NoError(t, err)
	n, err = a.Write([]byte("world"))
	require.Equal(t, 5, n)
	require.NoError(t, err)
	mu.Lock()
	artificialPacketDropChance = originalAPDC
	mu.Unlock()
	a.Close()
	all, err := ioutil.ReadAll(b)
	require.NoError(t, err)
	require.EqualValues(t, "helloworld", all)
}
コード例 #5
0
ファイル: pubsub_test.go プロジェクト: musha68k/go-ipfs
func TestCloseSubscription(t *testing.T) {
	ps := NewPubSub()
	ps.Publish(1)
	s := ps.Subscribe()
	select {
	case <-s.Values:
		t.FailNow()
	default:
	}
	ps.Publish(2)
	s2 := ps.Subscribe()
	ps.Publish(3)
	require.Equal(t, 2, <-s.Values)
	require.EqualValues(t, 3, <-s.Values)
	s.Close()
	_, ok := <-s.Values
	require.False(t, ok)
	ps.Publish(4)
	ps.Close()
	require.Equal(t, 3, <-s2.Values)
	require.Equal(t, 4, <-s2.Values)
	require.Nil(t, <-s2.Values)
	s2.Close()
}
コード例 #6
0
ファイル: utp_test.go プロジェクト: musha68k/go-ipfs
func TestMinMaxHeaderType(t *testing.T) {
	require.Equal(t, stSyn, stMax)
}
コード例 #7
0
ファイル: cache_test.go プロジェクト: musha68k/go-ipfs
func TestCache(t *testing.T) {
	td, err := ioutil.TempDir("", "gotest")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(td)
	c, err := NewCache(filepath.Join(td, "cache"))
	if err != nil {
		t.Fatal(err)
	}
	assert.EqualValues(t, 0, c.Info().Filled)
	c.WalkItems(func(i ItemInfo) {})
	_, err = c.OpenFile("/", os.O_CREATE)
	assert.NotNil(t, err)
	_, err = c.OpenFile("", os.O_CREATE)
	assert.NotNil(t, err)
	c.WalkItems(func(i ItemInfo) {})
	require.Equal(t, 0, c.Info().NumItems)
	_, err = c.OpenFile("notexist", 0)
	assert.True(t, os.IsNotExist(err), err)
	_, err = c.OpenFile("/notexist", 0)
	assert.True(t, os.IsNotExist(err), err)
	_, err = c.OpenFile("/dir/notexist", 0)
	assert.True(t, os.IsNotExist(err), err)
	f, err := c.OpenFile("dir/blah", os.O_CREATE)
	require.NoError(t, err)
	defer f.Close()
	c.WalkItems(func(i ItemInfo) {})
	assert.True(t, missinggo.FilePathExists(filepath.Join(td, filepath.FromSlash("cache/dir/blah"))))
	assert.True(t, missinggo.FilePathExists(filepath.Join(td, filepath.FromSlash("cache/dir/"))))
	assert.Equal(t, 1, c.Info().NumItems)
	f.Remove()
	assert.False(t, missinggo.FilePathExists(filepath.Join(td, filepath.FromSlash("dir/blah"))))
	assert.False(t, missinggo.FilePathExists(filepath.Join(td, filepath.FromSlash("dir/"))))
	_, err = f.Read(nil)
	assert.NotEqual(t, io.EOF, err)
	a, err := c.OpenFile("/a", os.O_CREATE|os.O_WRONLY)
	defer a.Close()
	require.Nil(t, err)
	b, err := c.OpenFile("b", os.O_CREATE|os.O_WRONLY)
	defer b.Close()
	require.Nil(t, err)
	c.mu.Lock()
	assert.True(t, c.pathInfo("a").Accessed.Before(c.pathInfo("b").Accessed))
	c.mu.Unlock()
	n, err := a.Write([]byte("hello"))
	assert.Nil(t, err)
	assert.EqualValues(t, 5, n)
	assert.EqualValues(t, 5, c.Info().Filled)
	assert.True(t, c.pathInfo("b").Accessed.Before(c.pathInfo("a").Accessed))
	c.SetCapacity(5)
	n, err = a.Write([]byte(" world"))
	assert.NotNil(t, err)
	_, err = b.Write([]byte("boom!"))
	// "a" and "b" have been evicted.
	assert.NotNil(t, err)
	assert.EqualValues(t, 0, c.Info().Filled)
	assert.EqualValues(t, 0, c.Info().NumItems)
	_, err = a.Seek(0, os.SEEK_SET)
	assert.NotNil(t, err)
}