|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package plugin |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "math/rand/v2" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestAddSameName(t *testing.T) { |
| 28 | + // name will have a random value to avoid conflicts |
| 29 | + pluginName := fmt.Sprintf("dummy-plugin-%d", rand.IntN(10000)) |
| 30 | + |
| 31 | + firstWasCancelled := false |
| 32 | + p := &Plugin{ |
| 33 | + name: pluginName, |
| 34 | + cancel: func(err error) { firstWasCancelled = true }, |
| 35 | + } |
| 36 | + |
| 37 | + // ensure the plugin we are using is registered |
| 38 | + draPlugins.add(p) |
| 39 | + defer draPlugins.delete(p.name) |
| 40 | + |
| 41 | + assert.False(t, firstWasCancelled, "should not cancel context after the first call") |
| 42 | + |
| 43 | + secondWasCancelled := false |
| 44 | + p2 := &Plugin{ |
| 45 | + name: pluginName, |
| 46 | + cancel: func(err error) { secondWasCancelled = true }, |
| 47 | + } |
| 48 | + |
| 49 | + draPlugins.add(p2) |
| 50 | + defer draPlugins.delete(p2.name) |
| 51 | + |
| 52 | + assert.True(t, firstWasCancelled, "should cancel context after the second call") |
| 53 | + assert.False(t, secondWasCancelled, "should not cancel context of a new plugin") |
| 54 | +} |
| 55 | + |
| 56 | +func TestDelete(t *testing.T) { |
| 57 | + pluginName := fmt.Sprintf("dummy-plugin-%d", rand.IntN(10000)) |
| 58 | + |
| 59 | + wasCancelled := false |
| 60 | + p := &Plugin{ |
| 61 | + name: pluginName, |
| 62 | + cancel: func(err error) { wasCancelled = true }, |
| 63 | + } |
| 64 | + |
| 65 | + // ensure the plugin we are using is registered |
| 66 | + draPlugins.add(p) |
| 67 | + |
| 68 | + draPlugins.delete(p.name) |
| 69 | + |
| 70 | + assert.True(t, wasCancelled, "should cancel context after the second call") |
| 71 | +} |
0 commit comments