2015-06-17 22:31:46 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-10-24 10:17:17 +00:00
|
|
|
package atomic
|
2015-06-17 22:31:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2015-10-24 10:17:17 +00:00
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2015-06-17 22:31:46 +00:00
|
|
|
)
|
|
|
|
|
2015-10-26 03:35:57 +00:00
|
|
|
func ExpectValue(t *testing.T, atomicValue *Value, expectedValue interface{}) {
|
2015-06-17 22:31:46 +00:00
|
|
|
actualValue := atomicValue.Load()
|
|
|
|
if actualValue != expectedValue {
|
2015-08-08 01:52:23 +00:00
|
|
|
t.Errorf("Expected to find %v, found %v", expectedValue, actualValue)
|
2015-06-17 22:31:46 +00:00
|
|
|
}
|
|
|
|
ch := make(chan interface{})
|
|
|
|
go func() {
|
|
|
|
ch <- atomicValue.Load()
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case actualValue = <-ch:
|
|
|
|
if actualValue != expectedValue {
|
2015-08-08 01:52:23 +00:00
|
|
|
t.Errorf("Expected to find %v, found %v", expectedValue, actualValue)
|
2015-06-17 22:31:46 +00:00
|
|
|
return
|
|
|
|
}
|
2015-10-24 10:17:17 +00:00
|
|
|
case <-time.After(util.ForeverTestTimeout):
|
2015-06-17 22:31:46 +00:00
|
|
|
t.Error("Value could not be read")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAtomicValue(t *testing.T) {
|
2015-10-26 03:35:57 +00:00
|
|
|
atomicValue := &Value{}
|
2015-06-17 22:31:46 +00:00
|
|
|
ExpectValue(t, atomicValue, nil)
|
|
|
|
atomicValue.Store(10)
|
|
|
|
ExpectValue(t, atomicValue, 10)
|
|
|
|
}
|