Skip to content

Commit 33c89be

Browse files
authored
chore: remove the extension sock file before start (#512)
Co-authored-by: rick <[email protected]>
1 parent a8c52fa commit 33c89be

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

pkg/extension/option.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2023 API Testing Authors.
2+
Copyright 2023-2024 API Testing Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -71,6 +71,10 @@ func (o *Extension) GetFullName() string {
7171

7272
func CreateRunner(ext *Extension, c *cobra.Command, remoteServer remote.LoaderServer) (err error) {
7373
protocol, address := ext.GetListenAddress()
74+
// remove the exist socket file
75+
if ext.Socket != "" {
76+
_ = os.Remove(ext.Socket)
77+
}
7478

7579
var lis net.Listener
7680
lis, err = net.Listen(protocol, address)
@@ -85,6 +89,7 @@ func CreateRunner(ext *Extension, c *cobra.Command, remoteServer remote.LoaderSe
8589

8690
RegisterStopSignal(c.Context(), func() {
8791
_ = os.Remove(ext.Socket)
92+
_ = lis.Close()
8893
}, gRPCServer)
8994

9095
err = gRPCServer.Serve(lis)
@@ -93,6 +98,10 @@ func CreateRunner(ext *Extension, c *cobra.Command, remoteServer remote.LoaderSe
9398

9499
func CreateMonitor(ext *Extension, c *cobra.Command, remoteServer monitor.MonitorServer) (err error) {
95100
protocol, address := ext.GetListenAddress()
101+
// remove the exist socket file
102+
if ext.Socket != "" {
103+
_ = os.Remove(ext.Socket)
104+
}
96105

97106
var lis net.Listener
98107
lis, err = net.Listen(protocol, address)
@@ -106,6 +115,7 @@ func CreateMonitor(ext *Extension, c *cobra.Command, remoteServer monitor.Monito
106115

107116
RegisterStopSignal(c.Context(), func() {
108117
_ = os.Remove(ext.Socket)
118+
_ = lis.Close()
109119
}, gRPCServer)
110120

111121
err = gRPCServer.Serve(lis)
@@ -114,6 +124,10 @@ func CreateMonitor(ext *Extension, c *cobra.Command, remoteServer monitor.Monito
114124

115125
func CreateExtensionRunner(ext *Extension, c *cobra.Command, remoteServer server.RunnerExtensionServer) (err error) {
116126
protocol, address := ext.GetListenAddress()
127+
// remove the exist socket file
128+
if ext.Socket != "" {
129+
_ = os.Remove(ext.Socket)
130+
}
117131

118132
var lis net.Listener
119133
lis, err = net.Listen(protocol, address)
@@ -127,6 +141,7 @@ func CreateExtensionRunner(ext *Extension, c *cobra.Command, remoteServer server
127141

128142
RegisterStopSignal(c.Context(), func() {
129143
_ = os.Remove(ext.Socket)
144+
_ = lis.Close()
130145
}, gRPCServer)
131146

132147
err = gRPCServer.Serve(lis)

pkg/extension/option_test.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2023 API Testing Authors.
2+
Copyright 2023-2024 API Testing Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -16,8 +16,13 @@ limitations under the License.
1616
package extension
1717

1818
import (
19+
"context"
20+
"os"
21+
"path/filepath"
1922
"testing"
23+
"time"
2024

25+
"github.com/spf13/cobra"
2126
"github.com/spf13/pflag"
2227
"github.com/stretchr/testify/assert"
2328
)
@@ -54,3 +59,78 @@ func TestExtension(t *testing.T) {
5459
assert.NotNil(t, flags.Lookup("port"))
5560
assert.NotNil(t, flags.Lookup("socket"))
5661
}
62+
63+
func TestCreateRunner(t *testing.T) {
64+
65+
t.Run("invalid port", func(t *testing.T) {
66+
extMgr := NewExtension("git", "store", 75530)
67+
extMgr.Port = 75530
68+
assert.NotNil(t, extMgr)
69+
assert.Error(t, CreateRunner(extMgr, nil, nil))
70+
assert.Error(t, CreateMonitor(extMgr, nil, nil))
71+
assert.Error(t, CreateExtensionRunner(extMgr, nil, nil))
72+
})
73+
74+
t.Run("random port", func(t *testing.T) {
75+
extMgr := NewExtension("git", "store", -1)
76+
77+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
78+
defer cancel()
79+
command := &cobra.Command{}
80+
command.SetContext(ctx)
81+
assert.Error(t, CreateRunner(extMgr, command, nil))
82+
})
83+
84+
t.Run("random port, CreateMonitor", func(t *testing.T) {
85+
extMgr := NewExtension("git", "store", -1)
86+
87+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
88+
defer cancel()
89+
command := &cobra.Command{}
90+
command.SetContext(ctx)
91+
assert.Error(t, CreateMonitor(extMgr, command, nil))
92+
})
93+
94+
t.Run("random port, CreateExtensionRunner", func(t *testing.T) {
95+
extMgr := NewExtension("git", "store", -1)
96+
97+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
98+
defer cancel()
99+
command := &cobra.Command{}
100+
command.SetContext(ctx)
101+
assert.Error(t, CreateExtensionRunner(extMgr, command, nil))
102+
})
103+
104+
t.Run("socket", func(t *testing.T) {
105+
extMgr := NewExtension("git", "store", -1)
106+
extMgr.Socket = filepath.Join(os.TempDir(), time.Microsecond.String())
107+
108+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
109+
defer cancel()
110+
command := &cobra.Command{}
111+
command.SetContext(ctx)
112+
assert.Error(t, CreateRunner(extMgr, command, nil))
113+
})
114+
115+
t.Run("socket, CreateMonitor", func(t *testing.T) {
116+
extMgr := NewExtension("git", "store", -1)
117+
extMgr.Socket = filepath.Join(os.TempDir(), time.Microsecond.String())
118+
119+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
120+
defer cancel()
121+
command := &cobra.Command{}
122+
command.SetContext(ctx)
123+
assert.Error(t, CreateMonitor(extMgr, command, nil))
124+
})
125+
126+
t.Run("socket, CreateExtensionRunner", func(t *testing.T) {
127+
extMgr := NewExtension("git", "store", -1)
128+
extMgr.Socket = filepath.Join(os.TempDir(), time.Microsecond.String())
129+
130+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
131+
defer cancel()
132+
command := &cobra.Command{}
133+
command.SetContext(ctx)
134+
assert.Error(t, CreateExtensionRunner(extMgr, command, nil))
135+
})
136+
}

0 commit comments

Comments
 (0)