| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package jobmgr |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "context" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | |
| 13 | "github.com/netdata/netdata/go/plugins/pkg/netdataapi" |
| 14 | "github.com/netdata/netdata/go/plugins/pkg/safewriter" |
| 15 | "github.com/netdata/netdata/go/plugins/plugin/framework/dyncfg" |
| 16 | "github.com/netdata/netdata/go/plugins/plugin/framework/functions" |
| 17 | ) |
| 18 | |
| 19 | func TestDyncfgConfig_ShutdownDoesNotQueue(t *testing.T) { |
| 20 | tests := map[string]struct { |
| 21 | fn functions.Function |
| 22 | }{ |
| 23 | "canceled context sends single 503 response": { |
| 24 | fn: functions.Function{ |
| 25 | UID: "shutdown", |
| 26 | Args: []string{"unknown:id", "schema"}, |
| 27 | }, |
| 28 | }, |
| 29 | } |
| 30 | |
| 31 | for name, tc := range tests { |
| 32 | t.Run(name, func(t *testing.T) { |
| 33 | var buf bytes.Buffer |
| 34 | |
| 35 | mgr := New(Config{PluginName: testPluginName}) |
| 36 | mgr.SetDyncfgResponder(dyncfg.NewResponder(netdataapi.New(safewriter.New(&buf)))) |
| 37 | |
| 38 | ctx, cancel := context.WithCancel(context.Background()) |
| 39 | cancel() |
| 40 | mgr.ctx = ctx |
| 41 | |
| 42 | mgr.dyncfgConfig(dyncfg.NewFunction(tc.fn)) |
| 43 | |
| 44 | // On shutdown, handler should respond once and stop. |
| 45 | assert.Equal(t, 1, strings.Count(buf.String(), "FUNCTION_RESULT_BEGIN shutdown")) |
| 46 | assert.Contains(t, buf.String(), "\"status\":503") |
| 47 | }) |
| 48 | } |
| 49 | } |