forked from aequitas/terraform-provider-transip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bumped dependencies to latest versions. Needed to integrate mutexkv b…
…ecause of deprecation from terraform-plugin-sdk.
- Loading branch information
Showing
6 changed files
with
164 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package transip | ||
|
||
import ( | ||
"log" | ||
"sync" | ||
) | ||
|
||
//// | ||
// The below methods are copied from terraform-plugin-sdk v1. | ||
// source: https://github.com/hashicorp/terraform-plugin-sdk/blob/v1-maint/helper/mutexkv/mutexkv.go | ||
//// | ||
|
||
// MutexKV is a simple key/value store for arbitrary mutexes. It can be used to | ||
// serialize changes across arbitrary collaborators that share knowledge of the | ||
// keys they must serialize on. | ||
// | ||
// Deprecated: This will be removed in v2 without replacement. If you need | ||
// its functionality, you can copy it or reference the v1 package. | ||
// | ||
// The initial use case is to let aws_security_group_rule resources serialize | ||
// their access to individual security groups based on SG ID. | ||
type MutexKV struct { | ||
lock sync.Mutex | ||
store map[string]*sync.Mutex | ||
} | ||
|
||
// Locks the mutex for the given key. Caller is responsible for calling Unlock | ||
// for the same key | ||
// | ||
// Deprecated: This will be removed in v2 without replacement. If you need | ||
// its functionality, you can copy it or reference the v1 package. | ||
func (m *MutexKV) Lock(key string) { | ||
log.Printf("[DEBUG] Locking %q", key) | ||
m.get(key).Lock() | ||
log.Printf("[DEBUG] Locked %q", key) | ||
} | ||
|
||
// Unlock the mutex for the given key. Caller must have called Lock for the same key first | ||
// | ||
// Deprecated: This will be removed in v2 without replacement. If you need | ||
// its functionality, you can copy it or reference the v1 package. | ||
func (m *MutexKV) Unlock(key string) { | ||
log.Printf("[DEBUG] Unlocking %q", key) | ||
m.get(key).Unlock() | ||
log.Printf("[DEBUG] Unlocked %q", key) | ||
} | ||
|
||
// Returns a mutex for the given key, no guarantee of its lock status | ||
func (m *MutexKV) get(key string) *sync.Mutex { | ||
m.lock.Lock() | ||
defer m.lock.Unlock() | ||
mutex, ok := m.store[key] | ||
if !ok { | ||
mutex = &sync.Mutex{} | ||
m.store[key] = mutex | ||
} | ||
return mutex | ||
} | ||
|
||
// Returns a properly initalized MutexKV | ||
// | ||
// Deprecated: This will be removed in v2 without replacement. If you need | ||
// its functionality, you can copy it or reference the v1 package. | ||
func NewMutexKV() *MutexKV { | ||
return &MutexKV{ | ||
store: make(map[string]*sync.Mutex), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package transip | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestMutexKVLock(t *testing.T) { | ||
mkv := NewMutexKV() | ||
|
||
mkv.Lock("foo") | ||
|
||
doneCh := make(chan struct{}) | ||
|
||
go func() { | ||
mkv.Lock("foo") | ||
close(doneCh) | ||
}() | ||
|
||
select { | ||
case <-doneCh: | ||
t.Fatal("Second lock was able to be taken. This shouldn't happen.") | ||
case <-time.After(50 * time.Millisecond): | ||
// pass | ||
} | ||
} | ||
|
||
func TestMutexKVUnlock(t *testing.T) { | ||
mkv := NewMutexKV() | ||
|
||
mkv.Lock("foo") | ||
mkv.Unlock("foo") | ||
|
||
doneCh := make(chan struct{}) | ||
|
||
go func() { | ||
mkv.Lock("foo") | ||
close(doneCh) | ||
}() | ||
|
||
select { | ||
case <-doneCh: | ||
// pass | ||
case <-time.After(50 * time.Millisecond): | ||
t.Fatal("Second lock blocked after unlock. This shouldn't happen.") | ||
} | ||
} | ||
|
||
func TestMutexKVDifferentKeys(t *testing.T) { | ||
mkv := NewMutexKV() | ||
|
||
mkv.Lock("foo") | ||
|
||
doneCh := make(chan struct{}) | ||
|
||
go func() { | ||
mkv.Lock("bar") | ||
close(doneCh) | ||
}() | ||
|
||
select { | ||
case <-doneCh: | ||
// pass | ||
case <-time.After(50 * time.Millisecond): | ||
t.Fatal("Second lock on a different key blocked. This shouldn't happen.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters