cycle_state_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package v1alpha1
  14. import (
  15. "testing"
  16. )
  17. type fakeData struct {
  18. data string
  19. }
  20. func (f *fakeData) Clone() StateData {
  21. copy := &fakeData{
  22. data: f.data,
  23. }
  24. return copy
  25. }
  26. func TestCycleStateClone(t *testing.T) {
  27. var key StateKey = "key"
  28. data1 := "value1"
  29. data2 := "value2"
  30. state := NewCycleState()
  31. originalValue := &fakeData{
  32. data: data1,
  33. }
  34. state.Write(key, originalValue)
  35. stateCopy := state.Clone()
  36. valueCopy, err := stateCopy.Read(key)
  37. if err != nil {
  38. t.Errorf("failed to read copied value: %v", err)
  39. }
  40. if v, ok := valueCopy.(*fakeData); ok && v.data != data1 {
  41. t.Errorf("clone failed, got %q, expected %q", v.data, data1)
  42. }
  43. originalValue.data = data2
  44. original, err := state.Read(key)
  45. if err != nil {
  46. t.Errorf("failed to read original value: %v", err)
  47. }
  48. if v, ok := original.(*fakeData); ok && v.data != data2 {
  49. t.Errorf("original value should change, got %q, expected %q", v.data, data2)
  50. }
  51. if v, ok := valueCopy.(*fakeData); ok && v.data != data1 {
  52. t.Errorf("cloned copy should not change, got %q, expected %q", v.data, data1)
  53. }
  54. }
  55. func TestCycleStateCloneNil(t *testing.T) {
  56. var state *CycleState
  57. stateCopy := state.Clone()
  58. if stateCopy != nil {
  59. t.Errorf("clone expected to be nil")
  60. }
  61. }