github.com/anjalikarhana/fabric@v2.1.1+incompatible/orderer/common/server/server_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package server
     8  
     9  import (
    10  	"context"
    11  	"io/ioutil"
    12  	"os"
    13  	"path/filepath"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/golang/protobuf/proto"
    18  	cb "github.com/hyperledger/fabric-protos-go/common"
    19  	ab "github.com/hyperledger/fabric-protos-go/orderer"
    20  	localconfig "github.com/hyperledger/fabric/orderer/common/localconfig"
    21  	"github.com/hyperledger/fabric/orderer/common/multichannel"
    22  	"github.com/hyperledger/fabric/protoutil"
    23  	"github.com/stretchr/testify/assert"
    24  	"google.golang.org/grpc"
    25  	"google.golang.org/grpc/peer"
    26  )
    27  
    28  func TestBroadcastNoPanic(t *testing.T) {
    29  	// Defer recovers from the panic
    30  	_ = (&server{}).Broadcast(nil)
    31  }
    32  
    33  func TestDeliverNoPanic(t *testing.T) {
    34  	// Defer recovers from the panic
    35  	_ = (&server{}).Deliver(nil)
    36  }
    37  
    38  type recvr interface {
    39  	Recv() (*cb.Envelope, error)
    40  }
    41  
    42  type mockSrv struct {
    43  	grpc.ServerStream
    44  	msg *cb.Envelope
    45  	err error
    46  }
    47  
    48  func (mockSrv) Context() context.Context {
    49  	return peer.NewContext(context.Background(), &peer.Peer{})
    50  }
    51  
    52  type mockBroadcastSrv mockSrv
    53  
    54  func (mbs *mockBroadcastSrv) Recv() (*cb.Envelope, error) {
    55  	return mbs.msg, mbs.err
    56  }
    57  
    58  func (mbs *mockBroadcastSrv) Send(br *ab.BroadcastResponse) error {
    59  	panic("Unimplemented")
    60  }
    61  
    62  type mockDeliverSrv mockSrv
    63  
    64  func (mds *mockDeliverSrv) CreateStatusReply(status cb.Status) proto.Message {
    65  	return &ab.DeliverResponse{
    66  		Type: &ab.DeliverResponse_Status{Status: status},
    67  	}
    68  }
    69  
    70  func (mds *mockDeliverSrv) CreateBlockReply(block *cb.Block) proto.Message {
    71  	return &ab.DeliverResponse{
    72  		Type: &ab.DeliverResponse_Block{Block: block},
    73  	}
    74  }
    75  
    76  func (mds *mockDeliverSrv) Recv() (*cb.Envelope, error) {
    77  	return mds.msg, mds.err
    78  }
    79  
    80  func (mds *mockDeliverSrv) Send(br *ab.DeliverResponse) error {
    81  	panic("Unimplemented")
    82  }
    83  
    84  func testMsgTrace(handler func(dir string, msg *cb.Envelope) recvr, t *testing.T) {
    85  	dir, err := ioutil.TempDir("", "TestMsgTrace")
    86  	if err != nil {
    87  		t.Fatalf("Could not create temp dir")
    88  	}
    89  	defer os.RemoveAll(dir)
    90  
    91  	msg := &cb.Envelope{Payload: []byte("somedata")}
    92  
    93  	r := handler(dir, msg)
    94  
    95  	rMsg, err := r.Recv()
    96  	assert.Equal(t, msg, rMsg)
    97  	assert.Nil(t, err)
    98  
    99  	var fileData []byte
   100  	for i := 0; i < 100; i++ {
   101  		// Writing the trace file is deliberately non-blocking, wait up to a second, checking every 10 ms to see if the file now exists.
   102  		time.Sleep(10 * time.Millisecond)
   103  		filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
   104  			assert.Nil(t, err)
   105  			if path == dir {
   106  				return nil
   107  			}
   108  			assert.Nil(t, fileData, "Should only be one file")
   109  			fileData, err = ioutil.ReadFile(path)
   110  			assert.Nil(t, err)
   111  			return nil
   112  		})
   113  		if fileData != nil {
   114  			break
   115  		}
   116  	}
   117  
   118  	assert.Equal(t, protoutil.MarshalOrPanic(msg), fileData)
   119  }
   120  
   121  func TestBroadcastMsgTrace(t *testing.T) {
   122  	testMsgTrace(func(dir string, msg *cb.Envelope) recvr {
   123  		return &broadcastMsgTracer{
   124  			AtomicBroadcast_BroadcastServer: &mockBroadcastSrv{
   125  				msg: msg,
   126  			},
   127  			msgTracer: msgTracer{
   128  				debug: &localconfig.Debug{
   129  					BroadcastTraceDir: dir,
   130  				},
   131  				function: "Broadcast",
   132  			},
   133  		}
   134  	}, t)
   135  }
   136  
   137  func TestDeliverMsgTrace(t *testing.T) {
   138  	testMsgTrace(func(dir string, msg *cb.Envelope) recvr {
   139  		return &deliverMsgTracer{
   140  			Receiver: &mockDeliverSrv{
   141  				msg: msg,
   142  			},
   143  			msgTracer: msgTracer{
   144  				debug: &localconfig.Debug{
   145  					DeliverTraceDir: dir,
   146  				},
   147  				function: "Deliver",
   148  			},
   149  		}
   150  	}, t)
   151  }
   152  
   153  func TestDeliverNoChannel(t *testing.T) {
   154  	r := &multichannel.Registrar{}
   155  	ds := &deliverSupport{Registrar: r}
   156  	chain := ds.GetChain("mychannel")
   157  	assert.Nil(t, chain)
   158  	assert.True(t, chain == nil)
   159  }