Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package app
import (
"context"
"testing"
"time"
"github.com/agiledragon/gomonkey/v2"
"github.com/smartystreets/goconvey/convey"
"container-manager/pkg/reset/domain"
)
func TestResetMgr_Work(t *testing.T) {
convey.Convey("Test ResetMgr Work", t, func() {
r := &ResetMgr{
resetCache: domain.GetNpuInResetCache(),
countCache: domain.NewFailedResetCountCache(),
}
convey.Convey("When context is done", func() {
ctx, cancel := context.WithCancel(context.Background())
done := make(chan bool)
go func() {
r.Work(ctx)
done <- true
}()
cancel()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("Work method did not exit on context cancellation")
}
})
convey.Convey("When ticker triggers processResetWork", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
callCount := 0
const (
testCallTimes = 2
testLoopInterval = time.Millisecond * 10
testWaitTime = time.Millisecond * 30
)
patches := gomonkey.ApplyPrivateMethod(r, "processResetWork", func() {
callCount++
if callCount >= testCallTimes {
cancel()
}
}).ApplyFuncReturn(time.NewTicker, time.NewTicker(testLoopInterval))
defer patches.Reset()
go r.Work(ctx)
time.Sleep(testWaitTime)
convey.So(callCount, convey.ShouldBeGreaterThanOrEqualTo, 1)
})
})
}
func TestResetMgr_ShutDown(t *testing.T) {
convey.Convey("Test ResetMgr ShutDown", t, func() {
r := &ResetMgr{}
convey.So(func() { r.ShutDown() }, convey.ShouldNotPanic)
})
}
func TestResetMgr_Name(t *testing.T) {
convey.Convey("Test ResetMgr Name", t, func() {
r := &ResetMgr{}
name := r.Name()
convey.So(name, convey.ShouldEqual, resetMgrModuleName)
})
}
func TestResetMgr_Init(t *testing.T) {
convey.Convey("Test ResetMgr Init", t, func() {
r := &ResetMgr{}
err := r.Init()
convey.So(err, convey.ShouldBeNil)
})
}
func TestNewResetMgr(t *testing.T) {
convey.Convey("Test NewResetMgr", t, func() {
module := NewResetMgr()
convey.So(module, convey.ShouldNotBeNil)
resetMgr, ok := module.(*ResetMgr)
convey.So(ok, convey.ShouldBeTrue)
convey.So(resetMgr.resetCache, convey.ShouldNotBeNil)
convey.So(resetMgr.countCache, convey.ShouldNotBeNil)
})
}