github.com/artyom/thrift@v0.0.0-20130902103359-388840a05deb/transport_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one
     3   * or more contributor license agreements. See the NOTICE file
     4   * distributed with this work for additional information
     5   * regarding copyright ownership. The ASF licenses this file
     6   * to you under the Apache License, Version 2.0 (the
     7   * "License"); you may not use this file except in compliance
     8   * with the License. You may obtain a copy of the License at
     9   *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied. See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  
    20  package thrift
    21  
    22  import (
    23  	"io"
    24  	"net"
    25  	"strconv"
    26  	"testing"
    27  )
    28  
    29  const TRANSPORT_BINARY_DATA_SIZE = 4096
    30  
    31  var (
    32  	transport_bdata []byte // test data for writing; same as data
    33  )
    34  
    35  func init() {
    36  	transport_bdata = make([]byte, TRANSPORT_BINARY_DATA_SIZE)
    37  	for i := 0; i < TRANSPORT_BINARY_DATA_SIZE; i++ {
    38  		transport_bdata[i] = byte((i + 'a') % 255)
    39  	}
    40  }
    41  
    42  func TransportTest(t *testing.T, writeTrans TTransport, readTrans TTransport) {
    43  	buf := make([]byte, TRANSPORT_BINARY_DATA_SIZE)
    44  	if !writeTrans.IsOpen() {
    45  		t.Fatalf("Transport %T not open: %s", writeTrans, writeTrans)
    46  	}
    47  	if !readTrans.IsOpen() {
    48  		t.Fatalf("Transport %T not open: %s", readTrans, readTrans)
    49  	}
    50  	_, err := writeTrans.Write(transport_bdata)
    51  	if err != nil {
    52  		t.Fatalf("Transport %T cannot write binary data of length %d: %s", writeTrans, len(transport_bdata), err)
    53  	}
    54  	err = writeTrans.Flush()
    55  	if err != nil {
    56  		t.Fatalf("Transport %T cannot flush write of binary data: %s", writeTrans, err)
    57  	}
    58  	n, err := io.ReadFull(readTrans, buf)
    59  	if err != nil {
    60  		t.Errorf("Transport %T cannot read binary data of length %d: %s", readTrans, TRANSPORT_BINARY_DATA_SIZE, err)
    61  	}
    62  	if n != TRANSPORT_BINARY_DATA_SIZE {
    63  		t.Errorf("Transport %T read only %d instead of %d bytes of binary data", readTrans, n, TRANSPORT_BINARY_DATA_SIZE)
    64  	}
    65  	for k, v := range buf {
    66  		if v != transport_bdata[k] {
    67  			t.Fatalf("Transport %T read %d instead of %d for index %d of binary data 2", readTrans, v, transport_bdata[k], k)
    68  		}
    69  	}
    70  	_, err = writeTrans.Write(transport_bdata)
    71  	if err != nil {
    72  		t.Fatalf("Transport %T cannot write binary data 2 of length %d: %s", writeTrans, len(transport_bdata), err)
    73  	}
    74  	err = writeTrans.Flush()
    75  	if err != nil {
    76  		t.Fatalf("Transport %T cannot flush write binary data 2: %s", writeTrans, err)
    77  	}
    78  	buf = make([]byte, TRANSPORT_BINARY_DATA_SIZE)
    79  	read := 1
    80  	for n = 0; n < TRANSPORT_BINARY_DATA_SIZE && read != 0; {
    81  		read, err = readTrans.Read(buf[n:])
    82  		if err != nil {
    83  			t.Errorf("Transport %T cannot read binary data 2 of total length %d from offset %d: %s", readTrans, TRANSPORT_BINARY_DATA_SIZE, n, err)
    84  		}
    85  		n += read
    86  	}
    87  	if n != TRANSPORT_BINARY_DATA_SIZE {
    88  		t.Errorf("Transport %T read only %d instead of %d bytes of binary data 2", readTrans, n, TRANSPORT_BINARY_DATA_SIZE)
    89  	}
    90  	for k, v := range buf {
    91  		if v != transport_bdata[k] {
    92  			t.Fatalf("Transport %T read %d instead of %d for index %d of binary data 2", readTrans, v, transport_bdata[k], k)
    93  		}
    94  	}
    95  }
    96  
    97  func CloseTransports(t *testing.T, readTrans TTransport, writeTrans TTransport) {
    98  	err := readTrans.Close()
    99  	if err != nil {
   100  		t.Errorf("Transport %T cannot close read transport: %s", readTrans, err)
   101  	}
   102  	if writeTrans != readTrans {
   103  		err = writeTrans.Close()
   104  		if err != nil {
   105  			t.Errorf("Transport %T cannot close write transport: %s", writeTrans, err)
   106  		}
   107  	}
   108  }
   109  
   110  func FindAvailableTCPServerPort(startPort int) (net.Addr, error) {
   111  	for i := startPort; i < 65535; i++ {
   112  		s := "127.0.0.1:" + strconv.Itoa(i)
   113  		l, err := net.Listen("tcp", s)
   114  		if err == nil {
   115  			l.Close()
   116  			return net.ResolveTCPAddr("tcp", s)
   117  		}
   118  	}
   119  	return nil, NewTTransportException(UNKNOWN_TRANSPORT_EXCEPTION, "Could not find available server port")
   120  }