Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
395d6964
Commit
395d6964
authored
Apr 10, 2015
by
Daniel Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix race
parent
6835318d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
32 deletions
+51
-32
controller_test.go
pkg/controller/framework/controller_test.go
+14
-21
fake_controller_source.go
pkg/controller/framework/fake_controller_source.go
+24
-3
deep_copy.go
pkg/conversion/deep_copy.go
+13
-8
No files found.
pkg/controller/framework/controller_test.go
View file @
395d6964
...
...
@@ -218,8 +218,9 @@ func TestHammerController(t *testing.T) {
go
controller
.
Run
(
stop
)
wg
:=
sync
.
WaitGroup
{}
for
i
:=
0
;
i
<
10
;
i
++
{
wg
.
Add
(
1
)
const
threads
=
3
wg
.
Add
(
threads
)
for
i
:=
0
;
i
<
threads
;
i
++
{
go
func
()
{
defer
wg
.
Done
()
// Let's add a few objects to the source.
...
...
@@ -227,7 +228,7 @@ func TestHammerController(t *testing.T) {
rs
:=
rand
.
NewSource
(
rand
.
Int63
())
f
:=
fuzz
.
New
()
.
NilChance
(
.5
)
.
NumElements
(
0
,
2
)
.
RandSource
(
rs
)
r
:=
rand
.
New
(
rs
)
// Mustn't use r and f concurrently!
for
i
:=
0
;
i
<
75
0
;
i
++
{
for
i
:=
0
;
i
<
10
0
;
i
++
{
var
name
string
var
isNew
bool
if
currentNames
.
Len
()
==
0
||
r
.
Intn
(
3
)
==
1
{
...
...
@@ -335,48 +336,40 @@ func TestUpdate(t *testing.T) {
}
}
var
wg
sync
.
WaitGroup
tests
:=
[]
func
(
string
){
func
(
name
string
)
{
defer
wg
.
Done
()
testDoneWG
.
Add
(
1
)
name
=
"a-"
+
name
source
.
Add
(
pod
(
name
,
FROM
))
source
.
Modify
(
pod
(
name
,
TO
))
},
func
(
name
string
)
{
defer
wg
.
Done
()
testDoneWG
.
Add
(
1
)
name
=
"b-"
+
name
source
.
Add
(
pod
(
name
,
FROM
))
source
.
ModifyDropWatch
(
pod
(
name
,
TO
))
},
func
(
name
string
)
{
defer
wg
.
Done
()
testDoneWG
.
Add
(
1
)
name
=
"c-"
+
name
source
.
AddDropWatch
(
pod
(
name
,
FROM
))
source
.
Modify
(
pod
(
name
,
ADD_MISSED
))
source
.
Modify
(
pod
(
name
,
TO
))
},
func
(
name
string
)
{
defer
wg
.
Done
()
testDoneWG
.
Add
(
1
)
name
=
"d-"
+
name
source
.
Add
(
pod
(
name
,
FROM
))
},
}
// run every test a few times, in parallel
fuzzer
:=
fuzz
.
New
()
for
i
:=
0
;
i
<
20
;
i
++
{
for
_
,
f
:=
range
tests
{
wg
.
Add
(
1
)
var
name
string
for
len
(
name
)
<
10
{
fuzzer
.
Fuzz
(
&
name
)
}
go
f
(
name
)
const
threads
=
3
var
wg
sync
.
WaitGroup
wg
.
Add
(
threads
*
len
(
tests
))
testDoneWG
.
Add
(
threads
*
len
(
tests
))
for
i
:=
0
;
i
<
threads
;
i
++
{
for
j
,
f
:=
range
tests
{
go
func
(
name
string
,
f
func
(
string
))
{
defer
wg
.
Done
()
f
(
name
)
}(
fmt
.
Sprintf
(
"%v-%v"
,
i
,
j
),
f
)
}
}
wg
.
Wait
()
...
...
pkg/controller/framework/fake_controller_source.go
View file @
395d6964
...
...
@@ -23,6 +23,7 @@ import (
"sync"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
...
...
@@ -125,8 +126,15 @@ func (f *FakeControllerSource) List() (runtime.Object, error) {
defer
f
.
lock
.
RUnlock
()
list
:=
make
([]
runtime
.
Object
,
0
,
len
(
f
.
items
))
for
_
,
obj
:=
range
f
.
items
{
// TODO: should copy obj first
list
=
append
(
list
,
obj
)
// Must make a copy to allow clients to modify the object.
// Otherwise, if they make a change and write it back, they
// will inadvertantly change the our canonical copy (in
// addition to racing with other clients).
objCopy
,
err
:=
conversion
.
DeepCopy
(
obj
)
if
err
!=
nil
{
return
nil
,
err
}
list
=
append
(
list
,
objCopy
.
(
runtime
.
Object
))
}
listObj
:=
&
api
.
List
{}
if
err
:=
runtime
.
SetList
(
listObj
,
list
);
err
!=
nil
{
...
...
@@ -151,7 +159,20 @@ func (f *FakeControllerSource) Watch(resourceVersion string) (watch.Interface, e
return
nil
,
err
}
if
rc
<
len
(
f
.
changes
)
{
return
f
.
broadcaster
.
WatchWithPrefix
(
f
.
changes
[
rc
:
]),
nil
changes
:=
[]
watch
.
Event
{}
for
_
,
c
:=
range
f
.
changes
[
rc
:
]
{
// Must make a copy to allow clients to modify the
// object. Otherwise, if they make a change and write
// it back, they will inadvertantly change the our
// canonical copy (in addition to racing with other
// clients).
objCopy
,
err
:=
conversion
.
DeepCopy
(
c
.
Object
)
if
err
!=
nil
{
return
nil
,
err
}
changes
=
append
(
changes
,
watch
.
Event
{
c
.
Type
,
objCopy
.
(
runtime
.
Object
)})
}
return
f
.
broadcaster
.
WatchWithPrefix
(
changes
),
nil
}
else
if
rc
>
len
(
f
.
changes
)
{
return
nil
,
errors
.
New
(
"resource version in the future not supported by this fake"
)
}
...
...
pkg/conversion/deep_copy.go
View file @
395d6964
...
...
@@ -17,22 +17,27 @@ limitations under the License.
package
conversion
import
(
"bytes"
"encoding/gob"
"reflect"
)
var
deepCopier
=
NewConverter
()
// DeepCopy makes a deep copy of source. Won't work for any private fields!
// For nil slices, will return 0-length slices. These are equivilent in
// basically every way except for the way that reflect.DeepEqual checks.
func
DeepCopy
(
source
interface
{})
(
interface
{},
error
)
{
src
:=
reflect
.
ValueOf
(
source
)
v
:=
reflect
.
New
(
src
.
Type
())
.
Elem
()
s
:=
&
scope
{
converter
:
deepCopier
,
v
:=
reflect
.
New
(
reflect
.
TypeOf
(
source
))
buff
:=
&
bytes
.
Buffer
{}
enc
:=
gob
.
NewEncoder
(
buff
)
dec
:=
gob
.
NewDecoder
(
buff
)
err
:=
enc
.
Encode
(
source
)
if
err
!=
nil
{
return
nil
,
err
}
if
err
:=
deepCopier
.
convert
(
src
,
v
,
s
);
err
!=
nil
{
err
=
dec
.
Decode
(
v
.
Interface
())
if
err
!=
nil
{
return
nil
,
err
}
return
v
.
Interface
(),
nil
return
v
.
Elem
()
.
Interface
(),
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment