Skip to content

test(coderd): close metricscache and avoid background context #7996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func newWithCloser(t testing.TB, options *Options) (*codersdk.Client, io.Closer)
}

func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.CancelFunc, *url.URL, *coderd.Options) {
t.Helper()

if options == nil {
options = &Options{}
}
Expand Down Expand Up @@ -402,6 +404,8 @@ func NewWithAPI(t testing.TB, options *Options) (*codersdk.Client, io.Closer, *c
// well with coderd testing. It registers the "echo" provisioner for
// quick testing.
func NewProvisionerDaemon(t testing.TB, coderAPI *coderd.API) io.Closer {
t.Helper()

echoClient, echoServer := provisionersdk.MemTransportPipe()
ctx, cancelFunc := context.WithCancel(context.Background())
t.Cleanup(func() {
Expand Down
1 change: 1 addition & 0 deletions coderd/metricscache/metricscache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ func TestCache_DeploymentStats(t *testing.T) {
cache := metricscache.New(db, slogtest.Make(t, nil), metricscache.Intervals{
DeploymentStats: testutil.IntervalFast,
})
defer cache.Close()

_, err := db.InsertWorkspaceAgentStat(context.Background(), database.InsertWorkspaceAgentStatParams{
ID: uuid.New(),
Expand Down
2 changes: 1 addition & 1 deletion coderd/workspaceapps/apptest/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func createWorkspaceWithApps(t *testing.T, client *codersdk.Client, orgID uuid.U
primaryAppHost, err := client.AppHost(appHostCtx)
require.NoError(t, err)
if primaryAppHost.Host != "" {
manifest, err := agentClient.Manifest(context.Background())
manifest, err := agentClient.Manifest(appHostCtx)
require.NoError(t, err)
proxyURL := fmt.Sprintf(
"http://{{port}}--%s--%s--%s%s",
Expand Down
16 changes: 13 additions & 3 deletions enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
// New constructs an Enterprise coderd API instance.
// This handler is designed to wrap the AGPL Coder code and
// layer Enterprise functionality on top as much as possible.
func New(ctx context.Context, options *Options) (*API, error) {
func New(ctx context.Context, options *Options) (_ *API, err error) {
if options.EntitlementsUpdateInterval == 0 {
options.EntitlementsUpdateInterval = 10 * time.Minute
}
Expand All @@ -59,6 +59,11 @@ func New(ctx context.Context, options *Options) (*API, error) {
AGPL: coderd.New(options.Options),
Options: options,
}
defer func() {
if err != nil {
_ = api.Close()
}
}()

api.AGPL.Options.SetUserGroups = api.setUserGroups

Expand Down Expand Up @@ -312,8 +317,12 @@ type API struct {

func (api *API) Close() error {
api.cancel()
_ = api.replicaManager.Close()
_ = api.derpMesh.Close()
if api.replicaManager != nil {
_ = api.replicaManager.Close()
}
if api.derpMesh != nil {
_ = api.derpMesh.Close()
}
return api.AGPL.Close()
}

Expand Down Expand Up @@ -410,6 +419,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
// is actually changing.
changed = false
} else {
_ = coordinator.Close()
coordinator = haCoordinator
}

Expand Down
5 changes: 3 additions & 2 deletions enterprise/coderd/coderdenttest/coderdenttest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd/coderdtest"
Expand Down Expand Up @@ -58,6 +57,8 @@ func New(t *testing.T, options *Options) *codersdk.Client {
}

func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, io.Closer, *coderd.API) {
t.Helper()

if options == nil {
options = &Options{}
}
Expand All @@ -77,7 +78,7 @@ func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, io.Closer, *c
Keys: Keys,
ProxyHealthInterval: options.ProxyHealthInterval,
})
assert.NoError(t, err)
require.NoError(t, err)
setHandler(coderAPI.AGPL.RootHandler)
var provisionerCloser io.Closer = nopcloser{}
if options.IncludeProvisionerDaemon {
Expand Down
12 changes: 11 additions & 1 deletion enterprise/replicasync/replicasync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func TestReplica(t *testing.T) {
db, pubsub := dbtestutil.NewDB(t)
closeChan := make(chan struct{}, 1)
cancel, err := pubsub.Subscribe(replicasync.PubsubEvent, func(ctx context.Context, message []byte) {
closeChan <- struct{}{}
select {
case closeChan <- struct{}{}:
default:
}
})
require.NoError(t, err)
defer cancel()
Expand Down Expand Up @@ -70,6 +73,8 @@ func TestReplica(t *testing.T) {
RelayAddress: "http://169.254.169.254",
})
require.NoError(t, err)
defer server.Close()

require.Len(t, server.Regional(), 1)
require.Equal(t, peer.ID, server.Regional()[0].ID)
require.Empty(t, server.Self().Error)
Expand Down Expand Up @@ -113,6 +118,8 @@ func TestReplica(t *testing.T) {
TLSConfig: tlsConfig,
})
require.NoError(t, err)
defer server.Close()

require.Len(t, server.Regional(), 1)
require.Equal(t, peer.ID, server.Regional()[0].ID)
require.Empty(t, server.Self().Error)
Expand All @@ -138,6 +145,8 @@ func TestReplica(t *testing.T) {
RelayAddress: "http://127.0.0.1:1",
})
require.NoError(t, err)
defer server.Close()

require.Len(t, server.Regional(), 1)
require.Equal(t, peer.ID, server.Regional()[0].ID)
require.NotEmpty(t, server.Self().Error)
Expand All @@ -152,6 +161,7 @@ func TestReplica(t *testing.T) {
defer cancelCtx()
server, err := replicasync.New(ctx, slogtest.Make(t, nil), db, pubsub, nil)
require.NoError(t, err)
defer server.Close()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
Expand Down