@@ -3,6 +3,7 @@ package autobuild_test
3
3
import (
4
4
"context"
5
5
"os"
6
+ "sync/atomic"
6
7
"testing"
7
8
"time"
8
9
@@ -25,6 +26,8 @@ import (
25
26
"github.com/coder/coder/v2/provisioner/echo"
26
27
"github.com/coder/coder/v2/provisionersdk/proto"
27
28
"github.com/coder/coder/v2/testutil"
29
+
30
+ enterpriseSchedule "github.com/coder/coder/v2/enterprise/coderd/schedule"
28
31
)
29
32
30
33
func TestExecutorAutostartOK (t * testing.T ) {
@@ -1062,6 +1065,52 @@ func TestExecutorInactiveWorkspace(t *testing.T) {
1062
1065
})
1063
1066
}
1064
1067
1068
+ func TestNotifications (t * testing.T ) {
1069
+ t .Parallel ()
1070
+
1071
+ t .Run ("Dormancy" , func (t * testing.T ) {
1072
+ t .Parallel ()
1073
+
1074
+ // Setup template with dormancy and create a workspace with it
1075
+ var (
1076
+ ticker = make (chan time.Time )
1077
+ statCh = make (chan autobuild.Stats )
1078
+ logger = slogtest .Make (t , & slogtest.Options {})
1079
+ notificationsEnqueuer = testutil.FakeNotificationsEnqueuer {}
1080
+ client = coderdtest .New (t , & coderdtest.Options {
1081
+ AutobuildTicker : ticker ,
1082
+ AutobuildStats : statCh ,
1083
+ IncludeProvisionerDaemon : true ,
1084
+ NotificationsEnqueuer : & notificationsEnqueuer ,
1085
+ TemplateScheduleStore : enterpriseSchedule .NewEnterpriseTemplateScheduleStore (userQuietHoursScheduleStore (), & notificationsEnqueuer , logger ),
1086
+ })
1087
+ admin = coderdtest .CreateFirstUser (t , client )
1088
+ version = coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
1089
+ timeTilDormant = 1000
1090
+ )
1091
+
1092
+ coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
1093
+ template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID , func (ctr * codersdk.CreateTemplateRequest ) {
1094
+ ctr .TimeTilDormantMillis = ptr .Ref (int64 (timeTilDormant ))
1095
+ })
1096
+ userClient , _ := coderdtest .CreateAnotherUser (t , client , admin .OrganizationID )
1097
+ workspace := coderdtest .CreateWorkspace (t , userClient , admin .OrganizationID , template .ID )
1098
+ coderdtest .AwaitWorkspaceBuildJobCompleted (t , userClient , workspace .LatestBuild .ID )
1099
+
1100
+ // Stop workspace
1101
+ workspace = coderdtest .MustTransitionWorkspace (t , client , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
1102
+ build := coderdtest .AwaitWorkspaceBuildJobCompleted (t , userClient , workspace .LatestBuild .ID )
1103
+
1104
+ // Wait for workspace to become dormant
1105
+ ticker <- build .Job .CompletedAt .Add (time .Millisecond * time .Duration (timeTilDormant ) * 2 )
1106
+ <- statCh
1107
+
1108
+ // Check that the workspace is dormant
1109
+ workspace = coderdtest .MustWorkspace (t , client , workspace .ID )
1110
+ require .NotNil (t , workspace .DormantAt )
1111
+ })
1112
+ }
1113
+
1065
1114
func mustProvisionWorkspace (t * testing.T , client * codersdk.Client , mut ... func (* codersdk.CreateWorkspaceRequest )) codersdk.Workspace {
1066
1115
t .Helper ()
1067
1116
user := coderdtest .CreateFirstUser (t , client )
@@ -1113,3 +1162,10 @@ func mustWorkspaceParameters(t *testing.T, client *codersdk.Client, workspaceID
1113
1162
func TestMain (m * testing.M ) {
1114
1163
goleak .VerifyTestMain (m )
1115
1164
}
1165
+
1166
+ func userQuietHoursScheduleStore () * atomic.Pointer [schedule.UserQuietHoursScheduleStore ] {
1167
+ store := schedule .NewAGPLUserQuietHoursScheduleStore ()
1168
+ p := & atomic.Pointer [schedule.UserQuietHoursScheduleStore ]{}
1169
+ p .Store (& store )
1170
+ return p
1171
+ }
0 commit comments