-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #629 from ably/fix/host-fallback-conditions
Fix/host fallback conditions
- Loading branch information
Showing
11 changed files
with
425 additions
and
101 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
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,74 @@ | ||
package ablyutil | ||
|
||
import ( | ||
"math/rand" | ||
"sort" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
|
||
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
||
func GenerateRandomString(length int) string { | ||
b := make([]byte, length) | ||
for i := range b { | ||
b[i] = charset[seededRand.Intn(len(charset))] | ||
} | ||
return string(b) | ||
} | ||
|
||
type HashSet map[string]struct{} // struct {} has zero space complexity | ||
|
||
func NewHashSet() HashSet { | ||
return make(HashSet) | ||
} | ||
|
||
func (s HashSet) Add(item string) { | ||
s[item] = struct{}{} | ||
} | ||
|
||
func (s HashSet) Remove(item string) { | ||
delete(s, item) | ||
} | ||
|
||
func (s HashSet) Has(item string) bool { | ||
_, ok := s[item] | ||
return ok | ||
} | ||
|
||
func Copy(list []string) []string { | ||
copiedList := make([]string, len(list)) | ||
copy(copiedList, list) | ||
return copiedList | ||
} | ||
|
||
func Sort(list []string) []string { | ||
copiedList := Copy(list) | ||
sort.Strings(copiedList) | ||
return copiedList | ||
} | ||
|
||
func Shuffle(list []string) []string { | ||
copiedList := Copy(list) | ||
if len(copiedList) <= 1 { | ||
return copiedList | ||
} | ||
rand.Seed(time.Now().UnixNano()) | ||
rand.Shuffle(len(copiedList), func(i, j int) { copiedList[i], copiedList[j] = copiedList[j], copiedList[i] }) | ||
return copiedList | ||
} | ||
|
||
func SliceContains(s []string, str string) bool { | ||
for _, v := range s { | ||
if v == str { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func Empty(s string) bool { | ||
return len(strings.TrimSpace(s)) == 0 | ||
} |
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,111 @@ | ||
package ablyutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ably/ably-go/ably/internal/ablyutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_string(t *testing.T) { | ||
t.Run("String array Shuffle", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
strList := []string{} | ||
shuffledList := ablyutil.Shuffle(strList) | ||
assert.Equal(t, strList, shuffledList) | ||
|
||
strList = []string{"a"} | ||
shuffledList = ablyutil.Shuffle(strList) | ||
assert.Equal(t, strList, shuffledList) | ||
|
||
strList = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"} | ||
shuffledList = ablyutil.Shuffle(strList) | ||
assert.NotEqual(t, strList, shuffledList) | ||
assert.Equal(t, ablyutil.Sort(strList), ablyutil.Sort(shuffledList)) | ||
}) | ||
|
||
t.Run("String array contains", func(t *testing.T) { | ||
t.Parallel() | ||
strarr := []string{"apple", "banana", "dragonfruit"} | ||
|
||
if !ablyutil.SliceContains(strarr, "apple") { | ||
t.Error("String array should contain apple") | ||
} | ||
if ablyutil.SliceContains(strarr, "orange") { | ||
t.Error("String array should not contain orange") | ||
} | ||
}) | ||
|
||
t.Run("Empty String", func(t *testing.T) { | ||
t.Parallel() | ||
str := "" | ||
if !ablyutil.Empty(str) { | ||
t.Error("String should be empty") | ||
} | ||
str = " " | ||
if !ablyutil.Empty(str) { | ||
t.Error("String should be empty") | ||
} | ||
str = "ab" | ||
if ablyutil.Empty(str) { | ||
t.Error("String should not be empty") | ||
} | ||
}) | ||
} | ||
|
||
func TestHashSet(t *testing.T) { | ||
t.Run("Add should not duplicate entries", func(t *testing.T) { | ||
hashSet := ablyutil.NewHashSet() | ||
hashSet.Add("apple") | ||
hashSet.Add("apple") | ||
assert.Len(t, hashSet, 1) | ||
|
||
hashSet.Add("banana") | ||
assert.Len(t, hashSet, 2) | ||
|
||
hashSet.Add("orange") | ||
assert.Len(t, hashSet, 3) | ||
|
||
hashSet.Add("banana") | ||
hashSet.Add("apple") | ||
hashSet.Add("orange") | ||
hashSet.Add("orange") | ||
|
||
assert.Len(t, hashSet, 3) | ||
}) | ||
|
||
t.Run("Should check if item is present", func(t *testing.T) { | ||
hashSet := ablyutil.NewHashSet() | ||
hashSet.Add("apple") | ||
hashSet.Add("orange") | ||
if !hashSet.Has("apple") { | ||
t.Fatalf("Set should contain apple") | ||
} | ||
if hashSet.Has("banana") { | ||
t.Fatalf("Set shouldm't contain banana") | ||
} | ||
if !hashSet.Has("orange") { | ||
t.Fatalf("Set should contain orange") | ||
} | ||
}) | ||
|
||
t.Run("Should remove element", func(t *testing.T) { | ||
hashSet := ablyutil.NewHashSet() | ||
hashSet.Add("apple") | ||
assert.Len(t, hashSet, 1) | ||
|
||
hashSet.Add("orange") | ||
assert.Len(t, hashSet, 2) | ||
|
||
hashSet.Remove("apple") | ||
assert.Len(t, hashSet, 1) | ||
|
||
if hashSet.Has("apple") { | ||
t.Fatalf("Set shouldm't contain apple") | ||
} | ||
hashSet.Remove("orange") | ||
assert.Len(t, hashSet, 0) | ||
|
||
}) | ||
} |
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
Oops, something went wrong.