@@ -112,6 +112,25 @@ func (h *testErrorHandler) HandlePanic(ctx context.Context, job *rivertype.JobRo
112112 return h .HandlePanicFunc (ctx , job , panicVal , trace )
113113}
114114
115+ type testPilotWithJobCallbacks struct {
116+ riverpilot.StandardPilot
117+
118+ JobBeginFunc func (ctx context.Context , job * rivertype.JobRow )
119+ JobEndFunc func (ctx context.Context , job * rivertype.JobRow )
120+ }
121+
122+ func (p * testPilotWithJobCallbacks ) JobBegin (ctx context.Context , job * rivertype.JobRow ) {
123+ if p .JobBeginFunc != nil {
124+ p .JobBeginFunc (ctx , job )
125+ }
126+ }
127+
128+ func (p * testPilotWithJobCallbacks ) JobEnd (ctx context.Context , job * rivertype.JobRow ) {
129+ if p .JobEndFunc != nil {
130+ p .JobEndFunc (ctx , job )
131+ }
132+ }
133+
115134func TestJobExecutor_Execute (t * testing.T ) {
116135 t .Parallel ()
117136
@@ -995,6 +1014,90 @@ func TestJobExecutor_Execute(t *testing.T) {
9951014 require .True (t , bundle .errorHandler .HandlePanicCalled )
9961015 })
9971016
1017+ t .Run ("PilotJobCallbacksJobBeginAndEnd" , func (t * testing.T ) {
1018+ t .Parallel ()
1019+
1020+ executor , bundle := setup (t )
1021+
1022+ var events []string
1023+
1024+ executor .Pilot = & testPilotWithJobCallbacks {
1025+ JobBeginFunc : func (ctx context.Context , job * rivertype.JobRow ) {
1026+ require .Equal (t , bundle .jobRow .ID , job .ID )
1027+ events = append (events , "begin" )
1028+ },
1029+ JobEndFunc : func (ctx context.Context , job * rivertype.JobRow ) {
1030+ require .Equal (t , bundle .jobRow .ID , job .ID )
1031+ events = append (events , "end" )
1032+ },
1033+ }
1034+ executor .WorkUnit = newWorkUnitFactoryWithCustomRetry (func () error {
1035+ events = append (events , "work" )
1036+ return nil
1037+ }, nil ).MakeUnit (bundle .jobRow )
1038+
1039+ executor .Execute (ctx )
1040+ riversharedtest .WaitOrTimeout (t , bundle .updateCh )
1041+
1042+ require .Equal (t , []string {"begin" , "work" , "end" }, events )
1043+ })
1044+
1045+ t .Run ("PilotJobCallbacksJobEndInvokedOnError" , func (t * testing.T ) {
1046+ t .Parallel ()
1047+
1048+ executor , bundle := setup (t )
1049+
1050+ var events []string
1051+
1052+ executor .Pilot = & testPilotWithJobCallbacks {
1053+ JobBeginFunc : func (ctx context.Context , job * rivertype.JobRow ) {
1054+ require .Equal (t , bundle .jobRow .ID , job .ID )
1055+ events = append (events , "begin" )
1056+ },
1057+ JobEndFunc : func (ctx context.Context , job * rivertype.JobRow ) {
1058+ require .Equal (t , bundle .jobRow .ID , job .ID )
1059+ events = append (events , "end" )
1060+ },
1061+ }
1062+ executor .WorkUnit = newWorkUnitFactoryWithCustomRetry (func () error {
1063+ events = append (events , "work" )
1064+ return errors .New ("work failed" )
1065+ }, nil ).MakeUnit (bundle .jobRow )
1066+
1067+ executor .Execute (ctx )
1068+ riversharedtest .WaitOrTimeout (t , bundle .updateCh )
1069+
1070+ require .Equal (t , []string {"begin" , "work" , "end" }, events )
1071+ })
1072+
1073+ t .Run ("PilotJobCallbacksJobEndInvokedOnPanic" , func (t * testing.T ) {
1074+ t .Parallel ()
1075+
1076+ executor , bundle := setup (t )
1077+
1078+ var events []string
1079+
1080+ executor .Pilot = & testPilotWithJobCallbacks {
1081+ JobBeginFunc : func (ctx context.Context , job * rivertype.JobRow ) {
1082+ require .Equal (t , bundle .jobRow .ID , job .ID )
1083+ events = append (events , "begin" )
1084+ },
1085+ JobEndFunc : func (ctx context.Context , job * rivertype.JobRow ) {
1086+ require .Equal (t , bundle .jobRow .ID , job .ID )
1087+ events = append (events , "end" )
1088+ },
1089+ }
1090+ executor .WorkUnit = newWorkUnitFactoryWithCustomRetry (func () error {
1091+ events = append (events , "work" )
1092+ panic ("panic val" )
1093+ }, nil ).MakeUnit (bundle .jobRow )
1094+
1095+ executor .Execute (ctx )
1096+ riversharedtest .WaitOrTimeout (t , bundle .updateCh )
1097+
1098+ require .Equal (t , []string {"begin" , "work" , "end" }, events )
1099+ })
1100+
9981101 t .Run ("CancelFuncCleanedUpEvenWithoutCancel" , func (t * testing.T ) {
9991102 t .Parallel ()
10001103
0 commit comments