subset_cluster_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package cluster // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/aerospikereceiver/cluster"
  4. import (
  5. "errors"
  6. "testing"
  7. as "github.com/aerospike/aerospike-client-go/v6"
  8. "github.com/stretchr/testify/require"
  9. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/aerospikereceiver/cluster/mocks"
  10. )
  11. func TestSubsetCluster_New(t *testing.T) {
  12. t.Parallel()
  13. nodes := []Node{
  14. mocks.NewNode(t),
  15. mocks.NewNode(t),
  16. }
  17. cPolicy := as.NewClientPolicy()
  18. hosts := []*as.Host{
  19. as.NewHost("testip", 3000),
  20. as.NewHost("testip", 4000),
  21. }
  22. authEnabled := true
  23. nodeFactoryPos := newMockNodeFactoryFunc(t)
  24. nodeFactoryPos.On("Execute", cPolicy, hosts[0], authEnabled).Return(nodes[0], nil)
  25. nodeFactoryPos.On("Execute", cPolicy, hosts[1], authEnabled).Return(nodes[1], nil)
  26. factoryFuncPos := func(policy *as.ClientPolicy, hosts *as.Host, authEnabled bool) (Node, error) {
  27. return nodeFactoryPos.Execute(policy, hosts, authEnabled)
  28. }
  29. nodeFactoryNeg := newMockNodeFactoryFunc(t)
  30. nodeFactoryNeg.On("Execute", cPolicy, hosts[0], authEnabled).Return(nodes[0], nil)
  31. nodeFactoryNeg.On("Execute", cPolicy, hosts[1], authEnabled).Return(nil, errors.New("invalid host"))
  32. factoryFuncNeg := func(policy *as.ClientPolicy, hosts *as.Host, authEnabled bool) (Node, error) {
  33. return nodeFactoryNeg.Execute(policy, hosts, authEnabled)
  34. }
  35. testCluster, err := newSubsetCluster(cPolicy, hosts, authEnabled, factoryFuncPos)
  36. require.NoError(t, err)
  37. nodeFactoryPos.AssertExpectations(t)
  38. require.Equal(t, len(testCluster.GetNodes()), len(nodes))
  39. _, err = newSubsetCluster(cPolicy, hosts, authEnabled, factoryFuncNeg)
  40. nodeFactoryNeg.AssertExpectations(t)
  41. require.EqualError(t, err, "invalid host")
  42. }
  43. func TestSubsetCluster_GetNodes(t *testing.T) {
  44. t.Parallel()
  45. nodes := []Node{
  46. mocks.NewNode(t),
  47. mocks.NewNode(t),
  48. }
  49. testCluster := SubsetCluster{
  50. nodes: nodes,
  51. }
  52. actualNodes := testCluster.GetNodes()
  53. require.Equal(t, len(actualNodes), len(nodes))
  54. }
  55. func TestSubsetCluster_Close(t *testing.T) {
  56. t.Parallel()
  57. n1 := mocks.NewNode(t)
  58. n1.On("Close").Return()
  59. n2 := mocks.NewNode(t)
  60. n2.On("Close").Return()
  61. nodes := []Node{
  62. n1,
  63. n2,
  64. }
  65. testCluster := SubsetCluster{
  66. nodes: nodes,
  67. }
  68. testCluster.Close()
  69. n1.AssertExpectations(t)
  70. n2.AssertExpectations(t)
  71. }