| 1 |
/* |
| 2 |
Copyright 2023 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 routine |
| 18 |
|
| 19 |
import ( |
| 20 |
"net/http" |
| 21 |
"net/http/httptest" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"k8s.io/apiserver/pkg/endpoints/request" |
| 25 |
) |
| 26 |
|
| 27 |
func TestExecutionWithRoutine(t *testing.T) { |
| 28 |
var executed bool |
| 29 |
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 30 |
t := TaskFrom(r.Context()) |
| 31 |
t.Func = func() { |
| 32 |
executed = true |
| 33 |
} |
| 34 |
}) |
| 35 |
ts := httptest.NewServer(WithRoutine(handler, func(_ *http.Request, _ *request.RequestInfo) bool { return true })) |
| 36 |
defer ts.Close() |
| 37 |
|
| 38 |
_, err := http.Get(ts.URL) |
| 39 |
if err != nil { |
| 40 |
t.Errorf("got unexpected error on request: %v", err) |
| 41 |
} |
| 42 |
if !executed { |
| 43 |
t.Error("expected to execute") |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func TestAppendTask(t *testing.T) { |
| 48 |
tests := []struct { |
| 49 |
name string |
| 50 |
existingTask bool |
| 51 |
taskAppended bool |
| 52 |
shouldExecute bool |
| 53 |
}{ |
| 54 |
{ |
| 55 |
name: "append task when existing", |
| 56 |
existingTask: true, |
| 57 |
taskAppended: true, |
| 58 |
shouldExecute: true, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "not append task when no existing tasks", |
| 62 |
existingTask: false, |
| 63 |
taskAppended: false, |
| 64 |
shouldExecute: false, |
| 65 |
}, |
| 66 |
} |
| 67 |
for _, test := range tests { |
| 68 |
t.Run(test.name, func(t *testing.T) { |
| 69 |
var executed, appended bool |
| 70 |
taskToAppend := func() { |
| 71 |
executed = true |
| 72 |
} |
| 73 |
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 74 |
ctx := r.Context() |
| 75 |
if test.existingTask { |
| 76 |
t := TaskFrom(ctx) |
| 77 |
t.Func = func() {} |
| 78 |
} |
| 79 |
|
| 80 |
appended = AppendTask(ctx, &Task{taskToAppend}) |
| 81 |
}) |
| 82 |
ts := httptest.NewServer(WithRoutine(handler, func(_ *http.Request, _ *request.RequestInfo) bool { return true })) |
| 83 |
defer ts.Close() |
| 84 |
|
| 85 |
_, err := http.Get(ts.URL) |
| 86 |
if err != nil { |
| 87 |
t.Errorf("got unexpected error on request: %v", err) |
| 88 |
} |
| 89 |
|
| 90 |
if test.taskAppended != appended { |
| 91 |
t.Errorf("expected taskAppended: %t, got: %t", test.taskAppended, executed) |
| 92 |
} |
| 93 |
|
| 94 |
if test.shouldExecute != executed { |
| 95 |
t.Errorf("expected shouldExecute: %t, got: %t", test.shouldExecute, executed) |
| 96 |
} |
| 97 |
}) |
| 98 |
} |
| 99 |
} |