chore: remove the extension sock file before start (#512)

Co-authored-by: rick <linuxsuren@users.noreply.github.com>
This commit is contained in:
Rick 2024-07-12 09:31:24 +08:00 committed by GitHub
parent a8c52fa891
commit 33c89be15c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 97 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
Copyright 2023 API Testing Authors.
Copyright 2023-2024 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -71,6 +71,10 @@ func (o *Extension) GetFullName() string {
func CreateRunner(ext *Extension, c *cobra.Command, remoteServer remote.LoaderServer) (err error) {
protocol, address := ext.GetListenAddress()
// remove the exist socket file
if ext.Socket != "" {
_ = os.Remove(ext.Socket)
}
var lis net.Listener
lis, err = net.Listen(protocol, address)
@ -85,6 +89,7 @@ func CreateRunner(ext *Extension, c *cobra.Command, remoteServer remote.LoaderSe
RegisterStopSignal(c.Context(), func() {
_ = os.Remove(ext.Socket)
_ = lis.Close()
}, gRPCServer)
err = gRPCServer.Serve(lis)
@ -93,6 +98,10 @@ func CreateRunner(ext *Extension, c *cobra.Command, remoteServer remote.LoaderSe
func CreateMonitor(ext *Extension, c *cobra.Command, remoteServer monitor.MonitorServer) (err error) {
protocol, address := ext.GetListenAddress()
// remove the exist socket file
if ext.Socket != "" {
_ = os.Remove(ext.Socket)
}
var lis net.Listener
lis, err = net.Listen(protocol, address)
@ -106,6 +115,7 @@ func CreateMonitor(ext *Extension, c *cobra.Command, remoteServer monitor.Monito
RegisterStopSignal(c.Context(), func() {
_ = os.Remove(ext.Socket)
_ = lis.Close()
}, gRPCServer)
err = gRPCServer.Serve(lis)
@ -114,6 +124,10 @@ func CreateMonitor(ext *Extension, c *cobra.Command, remoteServer monitor.Monito
func CreateExtensionRunner(ext *Extension, c *cobra.Command, remoteServer server.RunnerExtensionServer) (err error) {
protocol, address := ext.GetListenAddress()
// remove the exist socket file
if ext.Socket != "" {
_ = os.Remove(ext.Socket)
}
var lis net.Listener
lis, err = net.Listen(protocol, address)
@ -127,6 +141,7 @@ func CreateExtensionRunner(ext *Extension, c *cobra.Command, remoteServer server
RegisterStopSignal(c.Context(), func() {
_ = os.Remove(ext.Socket)
_ = lis.Close()
}, gRPCServer)
err = gRPCServer.Serve(lis)

View File

@ -1,5 +1,5 @@
/*
Copyright 2023 API Testing Authors.
Copyright 2023-2024 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -16,8 +16,13 @@ limitations under the License.
package extension
import (
"context"
"os"
"path/filepath"
"testing"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)
@ -54,3 +59,78 @@ func TestExtension(t *testing.T) {
assert.NotNil(t, flags.Lookup("port"))
assert.NotNil(t, flags.Lookup("socket"))
}
func TestCreateRunner(t *testing.T) {
t.Run("invalid port", func(t *testing.T) {
extMgr := NewExtension("git", "store", 75530)
extMgr.Port = 75530
assert.NotNil(t, extMgr)
assert.Error(t, CreateRunner(extMgr, nil, nil))
assert.Error(t, CreateMonitor(extMgr, nil, nil))
assert.Error(t, CreateExtensionRunner(extMgr, nil, nil))
})
t.Run("random port", func(t *testing.T) {
extMgr := NewExtension("git", "store", -1)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
command := &cobra.Command{}
command.SetContext(ctx)
assert.Error(t, CreateRunner(extMgr, command, nil))
})
t.Run("random port, CreateMonitor", func(t *testing.T) {
extMgr := NewExtension("git", "store", -1)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
command := &cobra.Command{}
command.SetContext(ctx)
assert.Error(t, CreateMonitor(extMgr, command, nil))
})
t.Run("random port, CreateExtensionRunner", func(t *testing.T) {
extMgr := NewExtension("git", "store", -1)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
command := &cobra.Command{}
command.SetContext(ctx)
assert.Error(t, CreateExtensionRunner(extMgr, command, nil))
})
t.Run("socket", func(t *testing.T) {
extMgr := NewExtension("git", "store", -1)
extMgr.Socket = filepath.Join(os.TempDir(), time.Microsecond.String())
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
command := &cobra.Command{}
command.SetContext(ctx)
assert.Error(t, CreateRunner(extMgr, command, nil))
})
t.Run("socket, CreateMonitor", func(t *testing.T) {
extMgr := NewExtension("git", "store", -1)
extMgr.Socket = filepath.Join(os.TempDir(), time.Microsecond.String())
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
command := &cobra.Command{}
command.SetContext(ctx)
assert.Error(t, CreateMonitor(extMgr, command, nil))
})
t.Run("socket, CreateExtensionRunner", func(t *testing.T) {
extMgr := NewExtension("git", "store", -1)
extMgr.Socket = filepath.Join(os.TempDir(), time.Microsecond.String())
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
command := &cobra.Command{}
command.SetContext(ctx)
assert.Error(t, CreateExtensionRunner(extMgr, command, nil))
})
}