1
0
mirror of https://github.com/rancher/steve.git synced 2025-06-18 11:17:49 +00:00
steve/pkg/schema/definitions/refresh_test.go

114 lines
3.0 KiB
Go
Raw Normal View History

2024-01-23 21:12:26 +00:00
package definitions
import (
"context"
"fmt"
2024-01-23 21:12:26 +00:00
"testing"
"time"
"github.com/rancher/steve/pkg/debounce"
"github.com/stretchr/testify/require"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiregv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
)
type refreshable struct {
refreshChannel chan struct{}
2024-01-23 21:12:26 +00:00
}
func (r *refreshable) Refresh() error {
r.refreshChannel <- struct{}{}
2024-01-23 21:12:26 +00:00
return nil
}
func Test_onChangeCRD(t *testing.T) {
t.Parallel()
refreshChannel := make(chan struct{}, 1)
defer close(refreshChannel)
internalRefresh := refreshable{
refreshChannel: refreshChannel,
}
2024-01-23 21:12:26 +00:00
refresher := debounce.DebounceableRefresher{
Refreshable: &internalRefresh,
}
refreshHandler := refreshHandler{
debounceRef: &refresher,
debounceDuration: time.Microsecond * 2,
2024-01-23 21:12:26 +00:00
}
input := apiextv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "test-crd",
},
}
output, err := refreshHandler.onChangeCRD("test-crd", &input)
require.Nil(t, err)
require.Equal(t, input, *output)
err = receiveWithTimeout(refreshChannel, time.Second*5)
require.NoError(t, err)
2024-01-23 21:12:26 +00:00
}
func Test_onChangeAPIService(t *testing.T) {
t.Parallel()
refreshChannel := make(chan struct{}, 1)
defer close(refreshChannel)
internalRefresh := refreshable{
refreshChannel: refreshChannel,
}
2024-01-23 21:12:26 +00:00
refresher := debounce.DebounceableRefresher{
Refreshable: &internalRefresh,
}
refreshHandler := refreshHandler{
debounceRef: &refresher,
debounceDuration: time.Microsecond * 2,
2024-01-23 21:12:26 +00:00
}
input := apiregv1.APIService{
ObjectMeta: metav1.ObjectMeta{
Name: "test-apiservice",
},
}
output, err := refreshHandler.onChangeAPIService("test-apiservice", &input)
require.Nil(t, err)
require.Equal(t, input, *output)
err = receiveWithTimeout(refreshChannel, time.Second*5)
require.NoError(t, err)
2024-01-23 21:12:26 +00:00
}
func Test_startBackgroundRefresh(t *testing.T) {
t.Parallel()
refreshChannel := make(chan struct{}, 1)
internalRefresh := refreshable{
refreshChannel: refreshChannel,
}
2024-01-23 21:12:26 +00:00
refresher := debounce.DebounceableRefresher{
Refreshable: &internalRefresh,
}
refreshHandler := refreshHandler{
debounceRef: &refresher,
debounceDuration: time.Microsecond * 2,
2024-01-23 21:12:26 +00:00
}
ctx, cancel := context.WithCancel(context.Background())
refreshHandler.startBackgroundRefresh(ctx, time.Microsecond*2)
err := receiveWithTimeout(refreshChannel, time.Second*5)
require.NoError(t, err)
// we want to stop the refresher before closing the channel to avoid errors
// since this just stops the background refresh from asking for a new refresh, we still
// need to wait for any currently debounced refreshes to finish
2024-01-23 21:12:26 +00:00
cancel()
time.Sleep(time.Second * 1)
close(refreshChannel)
}
func receiveWithTimeout(channel chan struct{}, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
select {
case <-channel:
return nil
case <-ctx.Done():
return fmt.Errorf("channel did not recieve value in timeout %d", timeout)
}
2024-01-23 21:12:26 +00:00
}