mapping_resolver_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package endpoints
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestMappingResolver_TryResolve(t *testing.T) {
  9. regionId := "cn-hangzhou"
  10. productId := "ecs"
  11. endpoint := GetEndpointFromMap(regionId, productId)
  12. assert.Equal(t, "", endpoint)
  13. AddEndpointMapping("cn-hangzhou", "Ecs", "unreachable.aliyuncs.com")
  14. endpoint = GetEndpointFromMap(regionId, productId)
  15. assert.Equal(t, "unreachable.aliyuncs.com", endpoint)
  16. }
  17. func Test_MappingResolveConcurrent(t *testing.T) {
  18. current := len(endpointMapping.endpoint)
  19. cnt := 50
  20. var wg sync.WaitGroup
  21. for i := 0; i < cnt; i++ {
  22. wg.Add(1)
  23. go func(k int) {
  24. defer wg.Done()
  25. endpoint := fmt.Sprintf("ecs#cn-hangzhou%d", k)
  26. for j := 0; j < 50; j++ {
  27. err := AddEndpointMapping(fmt.Sprintf("cn-hangzhou%d", k), "ecs", endpoint)
  28. assert.Nil(t, err)
  29. assert.Equal(t, endpoint, GetEndpointFromMap(fmt.Sprintf("cn-hangzhou%d", k), "ecs"))
  30. }
  31. }(i)
  32. }
  33. wg.Wait()
  34. assert.Equal(t, (current + cnt), len(endpointMapping.endpoint))
  35. // hit cache and concurrent get
  36. for i := 0; i < cnt; i++ {
  37. wg.Add(1)
  38. go func(k int) {
  39. defer wg.Done()
  40. endpoint := fmt.Sprintf("ecs#cn-hangzhou%d", k)
  41. for j := 0; j < cnt; j++ {
  42. assert.Equal(t, endpoint, GetEndpointFromMap(fmt.Sprintf("cn-hangzhou%d", k), "ecs"))
  43. err := AddEndpointMapping(fmt.Sprintf("cn-hangzhou%d", k), "ecs", endpoint)
  44. assert.Nil(t, err)
  45. }
  46. }(i)
  47. }
  48. wg.Wait()
  49. assert.Equal(t, (current + cnt), len(endpointMapping.endpoint))
  50. }