github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/cluster/clusterproviders/zk/misc_test.go (about)

     1  package zk
     2  
     3  import (
     4  	"log/slog"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/asynkron/protoactor-go/cluster"
     9  	"github.com/golang/mock/gomock"
    10  	"github.com/stretchr/testify/suite"
    11  )
    12  
    13  func (suite *MiscTestSuite) TestIntToStr() {
    14  	suite.Equal("100", intToStr(100))
    15  }
    16  
    17  func (suite *MiscTestSuite) TestStrToInt() {
    18  	suite.Equal(100, strToInt("100"))
    19  	suite.Equal(0, strToInt("str0"))
    20  }
    21  
    22  func (suite *MiscTestSuite) TestIsStrBlank() {
    23  	suite.True(isStrBlank(""))
    24  	suite.False(isStrBlank("e"))
    25  }
    26  
    27  func (suite *MiscTestSuite) TestFormatBaseKey() {
    28  	suite.Equal("/", formatBaseKey(""))
    29  	suite.Equal("/a/b/c", formatBaseKey("a/b/c"))
    30  	suite.Equal("/", formatBaseKey("/"))
    31  	suite.Equal("/a", formatBaseKey("a/"))
    32  }
    33  
    34  func (suite *MiscTestSuite) TestParseSeq() {
    35  	seq, err := parseSeq("/proto.actors/dev/my_cluster/_c_f4245284934bdaf384102b6fc233bd14-actor-0000000042")
    36  	suite.Nil(err)
    37  	suite.Equal(42, seq)
    38  }
    39  
    40  func (suite *MiscTestSuite) TestStringContains() {
    41  	suite.True(stringContains([]string{"a", "b"}, "b"))
    42  	suite.False(stringContains([]string{"a", "b"}, "c"))
    43  }
    44  
    45  func (suite *MiscTestSuite) TestMapString() {
    46  	orig := []string{"A", "B", "C"}
    47  	suite.ElementsMatch([]string{"a", "b", "c"}, mapString(orig, strings.ToLower))
    48  	suite.ElementsMatch([]string{"A", "B", "C"}, orig)
    49  }
    50  
    51  func (suite *MiscTestSuite) TestSafeRun() {
    52  	suite.NotPanics(func() { safeRun(slog.Default(), func() { panic("don't worry, should panic here") }) })
    53  }
    54  
    55  func (suite *MiscTestSuite) TestNode() {
    56  	node := NewNode("pod1", "192.168.0.1", 7788, []string{"kind1", "kind2"})
    57  	host, port := node.GetAddress()
    58  	suite.Equal("192.168.0.1", host)
    59  	suite.Equal(7788, port)
    60  
    61  	suite.Equal("192.168.0.1:7788", node.GetAddressString())
    62  
    63  	suite.True(NewNode("pod1", "", 0, nil).Equal(node))
    64  
    65  	_, ok := node.GetMeta("key")
    66  	suite.False(ok)
    67  
    68  	node.SetMeta(metaKeySeq, "100")
    69  	suite.Equal(100, node.GetSeq())
    70  
    71  	suite.Equal(&cluster.Member{
    72  		Id:    "pod1",
    73  		Host:  "192.168.0.1",
    74  		Port:  int32(7788),
    75  		Kinds: []string{"kind1", "kind2"},
    76  	}, node.MemberStatus())
    77  
    78  	data, err := node.Serialize()
    79  	suite.Nil(err)
    80  	suite.Equal(`{"id":"pod1","name":"pod1","host":"192.168.0.1","address":"192.168.0.1","port":7788,"kinds":["kind1","kind2"],"alive":true}`, string(data))
    81  
    82  	node2 := &Node{}
    83  	err = node2.Deserialize([]byte(`{"id":"pod1","name":"pod1","host":"192.168.0.1","address":"192.168.0.1","port":7788,"kinds":["kind1","kind2"],"alive":true}`))
    84  	suite.Nil(err)
    85  	node.Meta = nil
    86  	suite.Equal(node2, node)
    87  }
    88  
    89  type MiscTestSuite struct {
    90  	suite.Suite
    91  	ctrl *gomock.Controller
    92  }
    93  
    94  func (suite *MiscTestSuite) SetupTest() {
    95  	suite.ctrl = gomock.NewController(suite.T())
    96  }
    97  
    98  func (suite *MiscTestSuite) TearDownTest() {
    99  	suite.ctrl.Finish()
   100  }
   101  
   102  func TestMiscTestSuite(t *testing.T) {
   103  	suite.Run(t, new(MiscTestSuite))
   104  }