-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Allow for provider shutdown to take longer than a heartbeat. (#3001)
* 🐛 Allow for Shutdown to take longer than a heartbeat. Signed-off-by: Preslav <[email protected]> * Add a test for provider shutdown. Signed-off-by: Preslav <[email protected]> * Make grace period and interval configurable on the provider. Signed-off-by: Preslav <[email protected]> * Add extra assertions for isCloseOrShutdown. Signed-off-by: Preslav <[email protected]> --------- Signed-off-by: Preslav <[email protected]>
- Loading branch information
1 parent
b0d48e4
commit 8266a63
Showing
2 changed files
with
83 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package providers | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.mondoo.com/cnquery/v9/providers-sdk/v1/plugin" | ||
) | ||
|
||
type testPlugin struct { | ||
plugin.Service | ||
} | ||
|
||
func (t *testPlugin) Connect(req *plugin.ConnectReq, callback plugin.ProviderCallback) (*plugin.ConnectRes, error) { | ||
return nil, nil | ||
} | ||
|
||
func (t *testPlugin) MockConnect(req *plugin.ConnectReq, callback plugin.ProviderCallback) (*plugin.ConnectRes, error) { | ||
return nil, nil | ||
} | ||
|
||
func (t *testPlugin) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error) { | ||
return nil, nil | ||
} | ||
|
||
func (t *testPlugin) Shutdown(req *plugin.ShutdownReq) (*plugin.ShutdownRes, error) { | ||
// sleep more than the heartbeat interval to ensure that even if shutting down | ||
// the provider can still respond to heartbeats | ||
time.Sleep(10 * time.Second) | ||
return &plugin.ShutdownRes{}, nil | ||
} | ||
|
||
func (t *testPlugin) GetData(req *plugin.DataReq) (*plugin.DataRes, error) { | ||
return nil, nil | ||
} | ||
|
||
func (t *testPlugin) StoreData(req *plugin.StoreReq) (*plugin.StoreRes, error) { | ||
return nil, nil | ||
} | ||
|
||
func TestProviderShutdown(t *testing.T) { | ||
s := &RunningProvider{ | ||
Plugin: &testPlugin{}, | ||
interval: 500 * time.Millisecond, | ||
gracePeriod: 500 * time.Millisecond, | ||
} | ||
err := s.heartbeat() | ||
require.NoError(t, err) | ||
require.False(t, s.isCloseOrShutdown()) | ||
// the shutdown here takes 10 seconds, whereas the heartbeat interval is every second. | ||
// this means that this provider gets multiple heartbeats while shutting down | ||
err = s.Shutdown() | ||
require.NoError(t, err) | ||
require.True(t, s.isCloseOrShutdown()) | ||
} |