github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3em/agent/heartbeater_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  package agent
    21  
    22  import (
    23  	"net"
    24  	"sync"
    25  	"testing"
    26  	"time"
    27  
    28  	hb "github.com/m3db/m3/src/m3em/generated/proto/heartbeat"
    29  	xgrpc "github.com/m3db/m3/src/m3em/x/grpc"
    30  	"github.com/m3db/m3/src/x/instrument"
    31  
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func TestInvalidMessageType(t *testing.T) {
    36  	hbService := &mockHeartbeatServer{}
    37  	hbListener, err := net.Listen("tcp", "127.0.0.1:0")
    38  	hbServer := xgrpc.NewServer(nil)
    39  	require.NoError(t, err)
    40  	hb.RegisterHeartbeaterServer(hbServer, hbService)
    41  	go hbServer.Serve(hbListener)
    42  	defer hbServer.Stop()
    43  
    44  	var (
    45  		lock   sync.Mutex
    46  		recErr error
    47  	)
    48  	iopts := instrument.NewOptions()
    49  	opts := heartbeatOpts{
    50  		endpoint: hbListener.Addr().String(),
    51  		nowFn:    time.Now,
    52  		errorFn: func(err error) {
    53  			lock.Lock()
    54  			recErr = err
    55  			lock.Unlock()
    56  		},
    57  	}
    58  
    59  	h, err := newHeartbeater(mockRunner{}, opts, iopts)
    60  	require.NoError(t, err)
    61  	require.NoError(t, h.start(100*time.Millisecond))
    62  	defer h.stop()
    63  	invalidMsg := heartbeatMsg{
    64  		stop:             false,
    65  		processTerminate: false,
    66  		overwritten:      false,
    67  	}
    68  	h.msgChan <- invalidMsg
    69  	time.Sleep(300 * time.Millisecond)
    70  	lock.Lock()
    71  	require.Error(t, recErr)
    72  	lock.Unlock()
    73  }
    74  
    75  type mockRunner struct{}
    76  
    77  func (mr mockRunner) Running() bool {
    78  	return true
    79  }