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 domain
import (
"testing"
"github.com/smartystreets/goconvey/convey"
"ascend-common/common-utils/hwlog"
)
func TestMain(m *testing.M) {
err := hwlog.InitRunLogger(&hwlog.LogConfig{OnlyToStdout: true}, nil)
if err != nil {
panic(err)
}
m.Run()
}
func TestNpuInResetCache_New(t *testing.T) {
convey.Convey("Test GetNpuInResetCache", t, func() {
cache := GetNpuInResetCache()
convey.So(cache, convey.ShouldNotBeNil)
convey.So(cache.npuInResetCache, convey.ShouldNotBeNil)
convey.So(len(cache.npuInResetCache), convey.ShouldEqual, 0)
})
}
func getTestResetCache() *NpuInResetCache {
cache := GetNpuInResetCache()
testNpus := []int32{1, 2, 3}
cache.SetNpuInReset(testNpus...)
return cache
}
func TestNpuInResetCache_Set(t *testing.T) {
convey.Convey("Test SetNpuInReset", t, func() {
cache := getTestResetCache()
convey.So(cache.npuInResetCache, convey.ShouldContainKey, int32(1))
convey.So(cache.npuInResetCache, convey.ShouldContainKey, int32(2))
convey.So(cache.npuInResetCache, convey.ShouldContainKey, int32(3))
})
}
func TestNpuInResetCache_Get(t *testing.T) {
convey.Convey("Test ISNpuInReset", t, func() {
cache := getTestResetCache()
convey.So(cache.IsNpuInReset(1), convey.ShouldBeTrue)
})
}
func TestNpuInResetCache_Clear(t *testing.T) {
convey.Convey("Test ClearNpuInReset", t, func() {
cache := getTestResetCache()
testToClear := []int32{1, 3}
cache.ClearNpuInReset(testToClear...)
convey.So(cache.npuInResetCache, convey.ShouldNotContainKey, int32(1))
convey.So(cache.npuInResetCache, convey.ShouldContainKey, int32(2))
convey.So(cache.npuInResetCache, convey.ShouldNotContainKey, int32(3))
convey.So(len(cache.npuInResetCache), convey.ShouldEqual, 1)
})
}
func TestNpuInResetCache_DeepCopy(t *testing.T) {
convey.Convey("Test DeepCopy", t, func() {
cache := getTestResetCache()
copyCache := cache.DeepCopy()
convey.So(copyCache, convey.ShouldResemble, map[int32]struct{}{1: {}, 2: {}, 3: {}})
delete(copyCache, 1)
convey.So(cache.DeepCopy(), convey.ShouldContainKey, int32(1))
})
}
func TestFailedResetCountCache_New(t *testing.T) {
convey.Convey("Test NewFailedResetCountCache", t, func() {
cache := NewFailedResetCountCache()
convey.So(cache, convey.ShouldNotBeNil)
convey.So(cache.failedResetCountCache, convey.ShouldNotBeNil)
convey.So(len(cache.failedResetCountCache), convey.ShouldEqual, 0)
})
}
func TestFailedResetCountCache_Set(t *testing.T) {
convey.Convey("Test SetFailedResetCount", t, func() {
cache := NewFailedResetCountCache()
testId := int32(1)
testCount := 5
cache.SetFailedResetCount(testId, testCount)
convey.So(cache.failedResetCountCache[testId], convey.ShouldEqual, testCount)
})
}
func TestFailedResetCountCache_Get(t *testing.T) {
convey.Convey("Test GetFailedResetCount", t, func() {
convey.Convey("Should return count for existing phyId", func() {
cache := NewFailedResetCountCache()
testId := int32(1)
testCount := 3
cache.SetFailedResetCount(testId, testCount)
count := cache.GetFailedResetCount(testId)
convey.So(count, convey.ShouldEqual, testCount)
})
convey.Convey("Should return 0 for non-existing phyId", func() {
cache := NewFailedResetCountCache()
count := cache.GetFailedResetCount(999)
convey.So(count, convey.ShouldEqual, 0)
})
})
}
func TestFailedResetCountCache_GetAll(t *testing.T) {
convey.Convey("Test GetAllFailedResetCountNpuId", t, func() {
convey.Convey("Should return all phyIds", func() {
cache := NewFailedResetCountCache()
testDate := map[int32]int{1: 1, 2: 2}
for id, count := range testDate {
cache.SetFailedResetCount(id, count)
}
ids := cache.GetAllFailedResetCountNpuId()
convey.So(ids, convey.ShouldContain, int32(1))
convey.So(ids, convey.ShouldContain, int32(2))
convey.So(len(ids), convey.ShouldEqual, len(testDate))
})
convey.Convey("Should return empty slice for empty cache", func() {
cache := NewFailedResetCountCache()
ids := cache.GetAllFailedResetCountNpuId()
convey.So(ids, convey.ShouldBeEmpty)
})
})
}
func TestFailedResetCountCache_Clear(t *testing.T) {
convey.Convey("Test ClearFailedResetCount", t, func() {
cache := NewFailedResetCountCache()
testId := int32(1)
testCount := 5
cache.SetFailedResetCount(testId, testCount)
cache.ClearFailedResetCount(1)
convey.So(cache.failedResetCountCache, convey.ShouldNotContainKey, int32(1))
convey.So(len(cache.failedResetCountCache), convey.ShouldEqual, 0)
})
}