-
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.
- Loading branch information
1 parent
83e18dd
commit 85161fb
Showing
2 changed files
with
212 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package example_test | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/sivaosorg/unify4g" | ||
) | ||
|
||
func TestGenerateUUID(t *testing.T) { | ||
var wg sync.WaitGroup | ||
numTests := 100 | ||
uniqueIDs := make(map[string]struct{}) | ||
mu := sync.Mutex{} | ||
|
||
for i := 0; i < numTests; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
id, _ := unify4g.GenerateUUID() | ||
mu.Lock() | ||
if _, exists := uniqueIDs[id]; exists { | ||
t.Errorf("Duplicate ID generated: %s", id) | ||
} | ||
uniqueIDs[id] = struct{}{} | ||
mu.Unlock() | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
if len(uniqueIDs) != numTests { | ||
t.Errorf("Expected %d unique IDs but got %d", numTests, len(uniqueIDs)) | ||
} | ||
} | ||
|
||
func TestGenerateTimestampID(t *testing.T) { | ||
var wg sync.WaitGroup | ||
numTests := 100 | ||
uniqueIDs := make(map[string]struct{}) | ||
mu := sync.Mutex{} | ||
|
||
for i := 0; i < numTests; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
id := unify4g.GenerateTimestampID() | ||
mu.Lock() | ||
if _, exists := uniqueIDs[id]; exists { | ||
t.Errorf("Duplicate ID generated: %s", id) | ||
} | ||
uniqueIDs[id] = struct{}{} | ||
mu.Unlock() | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
if len(uniqueIDs) != numTests { | ||
t.Errorf("Expected %d unique IDs but got %d", numTests, len(uniqueIDs)) | ||
} | ||
} | ||
|
||
// Recommended | ||
func TestGenerateCryptoID(t *testing.T) { | ||
var wg sync.WaitGroup | ||
numTests := 100 | ||
uniqueIDs := make(map[string]struct{}) | ||
mu := sync.Mutex{} | ||
|
||
for i := 0; i < numTests; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
id := unify4g.GenerateCryptoID() | ||
mu.Lock() | ||
if _, exists := uniqueIDs[id]; exists { | ||
t.Errorf("Duplicate ID generated: %s", id) | ||
} | ||
uniqueIDs[id] = struct{}{} | ||
mu.Unlock() | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
if len(uniqueIDs) != numTests { | ||
t.Errorf("Expected %d unique IDs but got %d", numTests, len(uniqueIDs)) | ||
} | ||
} |
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