Trying to fix unstable unittest. Timing issue.
Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
Issue-ID: NONRTRIC-735
Change-Id: Ib5e586a495428df380e3e0d4e5101e7c9f4937b2
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
- golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
+ golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y=
-golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=
+golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
"github.com/stretchr/testify/require"
)
"github.com/stretchr/testify/require"
)
-func createHttpClientMock(t *testing.T, configuration *Config, wg *sync.WaitGroup, token JwtToken) *http.Client {
+func createHttpClientMock(t *testing.T, configuration *Config, token JwtToken) *http.Client {
assertions := require.New(t)
clientMock := NewTestClient(func(req *http.Request) *http.Response {
if req.URL.String() == configuration.AuthServiceUrl {
assertions := require.New(t)
clientMock := NewTestClient(func(req *http.Request) *http.Response {
if req.URL.String() == configuration.AuthServiceUrl {
assertions.Contains(body, "grant_type="+configuration.GrantType)
contentType := req.Header.Get("content-type")
assertions.Equal("application/x-www-form-urlencoded", contentType)
assertions.Contains(body, "grant_type="+configuration.GrantType)
contentType := req.Header.Get("content-type")
assertions.Equal("application/x-www-form-urlencoded", contentType)
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBuffer(toBody(token))),
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBuffer(toBody(token))),
accessToken := "Access_token" + fmt.Sprint(time.Now().UnixNano())
token := JwtToken{Access_token: accessToken, Expires_in: 7, Token_type: "Token_type"}
accessToken := "Access_token" + fmt.Sprint(time.Now().UnixNano())
token := JwtToken{Access_token: accessToken, Expires_in: 7, Token_type: "Token_type"}
- wg := sync.WaitGroup{}
- wg.Add(2) // Get token two times
- clientMock := createHttpClientMock(t, configuration, &wg, token)
+ clientMock := createHttpClientMock(t, configuration, token)
go periodicRefreshIwtToken(clientMock, context)
go periodicRefreshIwtToken(clientMock, context)
- if waitTimeout(&wg, 12*time.Second) {
- t.Error("Not all calls to server were made")
- t.Fail()
- }
+ waitForFile(configuration.AuthTokenOutputFileName, 30, t)
tokenFileContent, err := ioutil.ReadFile(configuration.AuthTokenOutputFileName)
check(err)
tokenFileContent, err := ioutil.ReadFile(configuration.AuthTokenOutputFileName)
check(err)
context.Running = false
}
context.Running = false
}
+func waitForFile(fileName string, maxTimeSeconds int, t *testing.T) bool {
+ for i := 1; i < maxTimeSeconds; i++ {
+ if _, err := os.Stat(fileName); err == nil {
+ return true
+ }
+ time.Sleep(time.Second)
+ }
+ t.Error("File not created: " + fileName)
+ t.Fail()
+ return false
+}
+
func TestStart(t *testing.T) {
assertions := require.New(t)
log.SetLevel(log.TraceLevel)
func TestStart(t *testing.T) {
assertions := require.New(t)
log.SetLevel(log.TraceLevel)
configuration.AuthTokenOutputFileName = "/tmp/authToken" + fmt.Sprint(time.Now().UnixNano())
configuration.CACertsPath = configuration.CertPath
context := NewContext(configuration)
configuration.AuthTokenOutputFileName = "/tmp/authToken" + fmt.Sprint(time.Now().UnixNano())
configuration.CACertsPath = configuration.CertPath
context := NewContext(configuration)
+ t.Cleanup(func() {
+ os.Remove(configuration.AuthTokenOutputFileName)
+ })