jupyter_hooks_test.go
1 // Copyright 2026 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package runtime 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/stretchr/testify/require" 22 23 "github.com/alibaba/opensandbox/execd/pkg/jupyter/execute" 24 ) 25 26 func TestDispatchExecutionResultHooks_ErrorSkipsComplete(t *testing.T) { 27 var ( 28 errorCalls int 29 completeCalls int 30 ) 31 32 req := &ExecuteCodeRequest{ 33 Hooks: ExecuteResultHook{ 34 OnExecuteError: func(_ *execute.ErrorOutput) { 35 errorCalls++ 36 }, 37 OnExecuteComplete: func(_ time.Duration) { 38 completeCalls++ 39 }, 40 }, 41 } 42 43 dispatchExecutionResultHooks(req, &execute.ExecutionResult{ 44 ExecutionTime: 35 * time.Millisecond, 45 Error: &execute.ErrorOutput{ 46 EName: "RuntimeError", 47 EValue: "boom", 48 }, 49 }) 50 51 require.Equal(t, 1, errorCalls) 52 require.Equal(t, 0, completeCalls) 53 } 54 55 func TestDispatchExecutionResultHooks_SuccessEmitsComplete(t *testing.T) { 56 var ( 57 errorCalls int 58 completeCalls int 59 ) 60 61 req := &ExecuteCodeRequest{ 62 Hooks: ExecuteResultHook{ 63 OnExecuteError: func(_ *execute.ErrorOutput) { 64 errorCalls++ 65 }, 66 OnExecuteComplete: func(_ time.Duration) { 67 completeCalls++ 68 }, 69 }, 70 } 71 72 dispatchExecutionResultHooks(req, &execute.ExecutionResult{ 73 ExecutionTime: 50 * time.Millisecond, 74 }) 75 76 require.Equal(t, 0, errorCalls) 77 require.Equal(t, 1, completeCalls) 78 }