Skip to content

Commit

Permalink
fix: use TestMain to handle setup and teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
poppoerika committed Jan 28, 2022
1 parent 53d01df commit 3bd8740
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions momento/simple_cache_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,30 @@ import (
var (
TestAuthToken = os.Getenv("TEST_AUTH_TOKEN")
TestCacheName = os.Getenv("TEST_CACHE_NAME")
client *ScsClient
err error
)

const (
BadToken = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJpbnRlZ3JhdGlvbiIsImNwIjoiY29udHJvbC5jZWxsLWFscGhhLWRldi5wcmVwcm9kLmEubW9tZW50b2hxLmNvbSIsImMiOiJjYWNoZS5jZWxsLWFscGhhLWRldi5wcmVwcm9kLmEubW9tZW50b2hxLmNvbSJ9.gdghdjjfjyehhdkkkskskmmls76573jnajhjjjhjdhnndy"
DefaultTtlSeconds = 60
)

func TestMain(m *testing.M) {
setUp(&testing.T{})
exitVal := m.Run()
cleanUp(client)

os.Exit(exitVal)
}

func setUp(t *testing.T) (*ScsClient, error) {
if TestAuthToken == "" {
t.Error("Integration tests require TEST_AUTH_TOKEN env var.")
} else if TestCacheName == "" {
t.Error("Integration tests require TEST_CACHE_NAME env var.")
} else {
client, err := SimpleCacheClient(&requests.SimpleCacheClientRequest{
client, err = SimpleCacheClient(&requests.SimpleCacheClientRequest{
AuthToken: TestAuthToken,
DefaultTtlSeconds: DefaultTtlSeconds,
})
Expand Down Expand Up @@ -63,7 +73,7 @@ func TestCreateCacheGetSetValueAndDeleteCache(t *testing.T) {
key := []byte(uuid.NewString())
value := []byte(uuid.NewString())

client, err := setUp(&testing.T{})
client, err = setUp(&testing.T{})
if err != nil {
t.Error("Set up error: " + err.Error())
}
Expand Down Expand Up @@ -112,5 +122,4 @@ func TestCreateCacheGetSetValueAndDeleteCache(t *testing.T) {
if deleteCacheErr != nil {
t.Error(deleteCacheErr.Error())
}
cleanUp(client)
}

0 comments on commit 3bd8740

Please sign in to comment.