directbuilder_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package resolver
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "testing"
  7. "git.i2edu.net/i2/go-zero/core/lang"
  8. "git.i2edu.net/i2/go-zero/core/mathx"
  9. "github.com/stretchr/testify/assert"
  10. "google.golang.org/grpc/resolver"
  11. )
  12. func TestDirectBuilder_Build(t *testing.T) {
  13. tests := []int{
  14. 0,
  15. 1,
  16. 2,
  17. subsetSize / 2,
  18. subsetSize,
  19. subsetSize * 2,
  20. }
  21. for _, test := range tests {
  22. test := test
  23. t.Run(strconv.Itoa(test), func(t *testing.T) {
  24. var servers []string
  25. for i := 0; i < test; i++ {
  26. servers = append(servers, fmt.Sprintf("localhost:%d", i))
  27. }
  28. var b directBuilder
  29. cc := new(mockedClientConn)
  30. _, err := b.Build(resolver.Target{
  31. Scheme: DirectScheme,
  32. Endpoint: strings.Join(servers, ","),
  33. }, cc, resolver.BuildOptions{})
  34. assert.Nil(t, err)
  35. size := mathx.MinInt(test, subsetSize)
  36. assert.Equal(t, size, len(cc.state.Addresses))
  37. m := make(map[string]lang.PlaceholderType)
  38. for _, each := range cc.state.Addresses {
  39. m[each.Addr] = lang.Placeholder
  40. }
  41. assert.Equal(t, size, len(m))
  42. })
  43. }
  44. }
  45. func TestDirectBuilder_Scheme(t *testing.T) {
  46. var b directBuilder
  47. assert.Equal(t, DirectScheme, b.Scheme())
  48. }