@@ -2516,6 +2516,93 @@ var _ = Describe("Fake client", func() {
25162516 Expect (cl .SubResource (subResourceScale ).Update (context .Background (), obj , client .WithSubResourceBody (scale )).Error ()).To (Equal (expectedErr ))
25172517 })
25182518
2519+ It ("is threadsafe" , func () {
2520+ cl := NewClientBuilder ().Build ()
2521+
2522+ u := func () * unstructured.Unstructured {
2523+ u := & unstructured.Unstructured {}
2524+ u .SetAPIVersion ("custom/v1" )
2525+ u .SetKind ("Version" )
2526+ u .SetName ("foo" )
2527+ return u
2528+ }
2529+
2530+ uList := func () * unstructured.UnstructuredList {
2531+ u := & unstructured.UnstructuredList {}
2532+ u .SetAPIVersion ("custom/v1" )
2533+ u .SetKind ("Version" )
2534+
2535+ return u
2536+ }
2537+
2538+ meta := func () * metav1.PartialObjectMetadata {
2539+ return & metav1.PartialObjectMetadata {
2540+ ObjectMeta : metav1.ObjectMeta {
2541+ Name : "foo" ,
2542+ Namespace : "default" ,
2543+ },
2544+ TypeMeta : metav1.TypeMeta {
2545+ APIVersion : "custom/v1" ,
2546+ Kind : "Version" ,
2547+ },
2548+ }
2549+ }
2550+ metaList := func () * metav1.PartialObjectMetadataList {
2551+ return & metav1.PartialObjectMetadataList {
2552+ TypeMeta : metav1.TypeMeta {
2553+
2554+ APIVersion : "custom/v1" ,
2555+ Kind : "Version" ,
2556+ },
2557+ }
2558+ }
2559+
2560+ pod := func () * corev1.Pod {
2561+ return & corev1.Pod {ObjectMeta : metav1.ObjectMeta {
2562+ Name : "foo" ,
2563+ Namespace : "default" ,
2564+ }}
2565+ }
2566+
2567+ ctx := context .Background ()
2568+ ops := []func (){
2569+ func () { _ = cl .Create (ctx , u ()) },
2570+ func () { _ = cl .Get (ctx , client .ObjectKeyFromObject (u ()), u ()) },
2571+ func () { _ = cl .Update (ctx , u ()) },
2572+ func () { _ = cl .Patch (ctx , u (), client .RawPatch (types .StrategicMergePatchType , []byte ("foo" ))) },
2573+ func () { _ = cl .Delete (ctx , u ()) },
2574+ func () { _ = cl .DeleteAllOf (ctx , u (), client.HasLabels {"foo" }) },
2575+ func () { _ = cl .List (ctx , uList ()) },
2576+
2577+ func () { _ = cl .Create (ctx , meta ()) },
2578+ func () { _ = cl .Get (ctx , client .ObjectKeyFromObject (meta ()), meta ()) },
2579+ func () { _ = cl .Update (ctx , meta ()) },
2580+ func () { _ = cl .Patch (ctx , meta (), client .RawPatch (types .StrategicMergePatchType , []byte ("foo" ))) },
2581+ func () { _ = cl .Delete (ctx , meta ()) },
2582+ func () { _ = cl .DeleteAllOf (ctx , meta (), client.HasLabels {"foo" }) },
2583+ func () { _ = cl .List (ctx , metaList ()) },
2584+
2585+ func () { _ = cl .Create (ctx , pod ()) },
2586+ func () { _ = cl .Get (ctx , client .ObjectKeyFromObject (pod ()), pod ()) },
2587+ func () { _ = cl .Update (ctx , pod ()) },
2588+ func () { _ = cl .Patch (ctx , pod (), client .RawPatch (types .StrategicMergePatchType , []byte ("foo" ))) },
2589+ func () { _ = cl .Delete (ctx , pod ()) },
2590+ func () { _ = cl .DeleteAllOf (ctx , pod (), client.HasLabels {"foo" }) },
2591+ func () { _ = cl .List (ctx , & corev1.PodList {}) },
2592+ }
2593+
2594+ wg := sync.WaitGroup {}
2595+ wg .Add (len (ops ))
2596+ for _ , op := range ops {
2597+ go func () {
2598+ defer wg .Done ()
2599+ op ()
2600+ }()
2601+ }
2602+
2603+ wg .Wait ()
2604+ })
2605+
25192606 scalableObjs := []client.Object {
25202607 & appsv1.Deployment {
25212608 ObjectMeta : metav1.ObjectMeta {
@@ -2604,6 +2691,7 @@ var _ = Describe("Fake client", func() {
26042691 scaleExpected .ResourceVersion = scaleActual .ResourceVersion
26052692 Expect (cmp .Diff (scaleExpected , scaleActual )).To (BeEmpty ())
26062693 })
2694+
26072695 }
26082696})
26092697
0 commit comments