Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent race condition in paralled testing #507

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions internal/conn/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package conn
import (
"fmt"
"os"
"sync"

"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/vserver"

Expand All @@ -16,7 +17,7 @@ type Region struct {
RegionName *string `json:"regionName,omitempty"`
}

var regionCacheByCode = make(map[string]Region)
var regionCacheByCode = sync.Map{}

func ParseRegionNoParameter(d *schema.ResourceData) (*string, error) {
if regionCode, regionCodeOk := d.GetOk("region"); regionCodeOk {
Expand All @@ -40,8 +41,8 @@ func ParseRegionNoParameter(d *schema.ResourceData) (*string, error) {
}

func GetRegionNoByCode(code string) *string {
if region, ok := regionCacheByCode[code]; ok {
return region.RegionNo
if region, ok := regionCacheByCode.Load(code); ok {
return region.(Region).RegionNo
}
return nil
}
Expand Down Expand Up @@ -86,7 +87,7 @@ func SetRegionCache(client *NcloudAPIClient, supportVPC bool) error {
region.RegionNo = r.RegionNo
}

regionCacheByCode[*region.RegionCode] = region
regionCacheByCode.Store(*region.RegionCode, region)
}

return nil
Expand Down Expand Up @@ -129,6 +130,6 @@ func getVpcRegionList(client *NcloudAPIClient) ([]*Region, error) {
}

func IsValidRegionCode(code string) bool {
_, ok := regionCacheByCode[code]
_, ok := regionCacheByCode.Load(code)
return ok
}
10 changes: 6 additions & 4 deletions internal/zone/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"reflect"

"sync"

"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
. "github.com/terraform-providers/terraform-provider-ncloud/internal/common"
Expand All @@ -19,7 +21,7 @@ type Zone struct {
RegionCode *string `json:"regionCode,omitempty"`
}

var zoneCache = make(map[string]string)
var zoneCache = sync.Map{}

func ParseZoneNoParameter(config *conn.ProviderConfig, d *schema.ResourceData) (*string, error) {
if zoneCode, zoneCodeOk := d.GetOk("zone"); zoneCodeOk {
Expand All @@ -34,11 +36,11 @@ func ParseZoneNoParameter(config *conn.ProviderConfig, d *schema.ResourceData) (
}

func GetZoneNoByCode(config *conn.ProviderConfig, code string) string {
if zoneNo := zoneCache[code]; zoneNo != "" {
return zoneNo
if zoneNo, ok := zoneCache.Load(code); ok {
return zoneNo.(string)
}
if zone, err := GetZoneByCode(config, code); err == nil && zone != nil {
zoneCache[code] = *zone.ZoneNo
zoneCache.Store(code, *zone.ZoneNo)
return *zone.ZoneNo
}
return ""
Expand Down
Loading