github.com/anjalikarhana/fabric@v2.1.1+incompatible/integration/e2e/e2e_suite_test.go (about)

     1  /*
     2  Copyright IBM Corp All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package e2e
     8  
     9  import (
    10  	"encoding/json"
    11  	"io"
    12  	"net"
    13  	"sync"
    14  	"testing"
    15  
    16  	"github.com/hyperledger/fabric/integration"
    17  	"github.com/hyperledger/fabric/integration/nwo"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	"github.com/onsi/gomega/gbytes"
    21  )
    22  
    23  func TestEndToEnd(t *testing.T) {
    24  	RegisterFailHandler(Fail)
    25  	RunSpecs(t, "EndToEnd Suite")
    26  }
    27  
    28  var (
    29  	buildServer *nwo.BuildServer
    30  	components  *nwo.Components
    31  )
    32  
    33  var _ = SynchronizedBeforeSuite(func() []byte {
    34  	buildServer = nwo.NewBuildServer()
    35  	buildServer.Serve()
    36  
    37  	components = buildServer.Components()
    38  	payload, err := json.Marshal(components)
    39  	Expect(err).NotTo(HaveOccurred())
    40  
    41  	return payload
    42  }, func(payload []byte) {
    43  	err := json.Unmarshal(payload, &components)
    44  	Expect(err).NotTo(HaveOccurred())
    45  })
    46  
    47  var _ = SynchronizedAfterSuite(func() {
    48  }, func() {
    49  	buildServer.Shutdown()
    50  })
    51  
    52  func StartPort() int {
    53  	return integration.E2EBasePort.StartPortForNode()
    54  }
    55  
    56  type DatagramReader struct {
    57  	buffer    *gbytes.Buffer
    58  	errCh     chan error
    59  	sock      *net.UDPConn
    60  	doneCh    chan struct{}
    61  	closeOnce sync.Once
    62  	err       error
    63  }
    64  
    65  func NewDatagramReader() *DatagramReader {
    66  	udpAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
    67  	Expect(err).NotTo(HaveOccurred())
    68  	sock, err := net.ListenUDP("udp", udpAddr)
    69  	Expect(err).NotTo(HaveOccurred())
    70  	err = sock.SetReadBuffer(1024 * 1024)
    71  	Expect(err).NotTo(HaveOccurred())
    72  
    73  	return &DatagramReader{
    74  		buffer: gbytes.NewBuffer(),
    75  		sock:   sock,
    76  		errCh:  make(chan error, 1),
    77  		doneCh: make(chan struct{}),
    78  	}
    79  }
    80  
    81  func (dr *DatagramReader) Buffer() *gbytes.Buffer {
    82  	return dr.buffer
    83  }
    84  
    85  func (dr *DatagramReader) Address() string {
    86  	return dr.sock.LocalAddr().String()
    87  }
    88  
    89  func (dr *DatagramReader) String() string {
    90  	return string(dr.buffer.Contents())
    91  }
    92  
    93  func (dr *DatagramReader) Start() {
    94  	buf := make([]byte, 1024*1024)
    95  	for {
    96  		select {
    97  		case <-dr.doneCh:
    98  			dr.errCh <- nil
    99  			return
   100  
   101  		default:
   102  			n, _, err := dr.sock.ReadFrom(buf)
   103  			if err != nil {
   104  				dr.errCh <- err
   105  				return
   106  			}
   107  			_, err = dr.buffer.Write(buf[0:n])
   108  			if err != nil {
   109  				dr.errCh <- err
   110  				return
   111  			}
   112  		}
   113  	}
   114  }
   115  
   116  func (dr *DatagramReader) Close() error {
   117  	dr.closeOnce.Do(func() {
   118  		close(dr.doneCh)
   119  		err := dr.sock.Close()
   120  		dr.err = <-dr.errCh
   121  		if dr.err == nil && err != nil && err != io.EOF {
   122  			dr.err = err
   123  		}
   124  	})
   125  	return dr.err
   126  }