Skip to content

Commit

Permalink
Create wrappers for cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stroiman committed Dec 5, 2024
1 parent c01c392 commit e389092
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,53 @@ func (r *consoleAPIMessageRecorder) ConsoleAPIMessage(msg v8.ConsoleAPIMessage)
r.messages = append(r.messages, msg)
}

type IsolateWithInspector struct {
iso *v8.Isolate
inspector *v8.Inspector
inspectorClient *v8.InspectorClient
}

func NewIsolateWithInspectorClient(handler v8.ConsoleAPIMessageHandler) *IsolateWithInspector {
iso := v8.NewIsolate()
client := v8.NewInspectorClient(handler)
inspector := v8.NewInspector(iso, client)
return &IsolateWithInspector{
iso,
inspector,
client,
}
}

func (iso *IsolateWithInspector) Dispose() {
iso.inspector.Dispose()
iso.inspectorClient.Dispose()
iso.iso.Dispose()
}

type ContextWithInspector struct {
*v8.Context
iso *IsolateWithInspector
}

func (iso *IsolateWithInspector) NewContext() *ContextWithInspector {
context := v8.NewContext(iso.iso)
iso.inspector.ContextCreated(context)
return &ContextWithInspector{context, iso}
}

func (ctx *ContextWithInspector) Dispose() {
ctx.iso.inspector.ContextDestroyed(ctx.Context)
ctx.Context.Close()
}

func TestMonitorCreateDispose(t *testing.T) {
recorder := consoleAPIMessageRecorder{}
t.Parallel()
iso := v8.NewIsolate()
recorder := consoleAPIMessageRecorder{}
iso := NewIsolateWithInspectorClient(&recorder)
defer iso.Dispose()
client := v8.NewInspectorClient(&recorder)
defer client.Dispose()
inspector := v8.NewInspector(iso, client)
defer inspector.Dispose()
context := v8.NewContext(iso)
defer context.Close()
inspector.ContextCreated(context)
defer inspector.ContextDestroyed(context)
context := iso.NewContext()
defer context.Dispose()

_, err := context.RunScript("console.log('Hello, world!'); console.error('Error, world!');", "")
if err != nil {
t.Error("Error occurred: " + err.Error())
Expand Down

0 comments on commit e389092

Please sign in to comment.