forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_env.go
102 lines (85 loc) · 1.93 KB
/
server_env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package scenarios
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
"v2ray.com/core/common/log"
"fmt"
"io/ioutil"
"sync"
_ "v2ray.com/core"
"v2ray.com/core/common/retry"
)
var (
runningServers = make([]*exec.Cmd, 0, 10)
testBinaryPath string
testBinaryPathGen sync.Once
)
func GenTestBinaryPath() {
testBinaryPathGen.Do(func() {
var tempDir string
err := retry.Timed(5, 100).On(func() error {
dir, err := ioutil.TempDir("", "v2ray")
if err != nil {
return err
}
tempDir = dir
return nil
})
if err != nil {
panic(err)
}
file := filepath.Join(tempDir, "v2ray.test")
if runtime.GOOS == "windows" {
file += ".exe"
}
testBinaryPath = file
fmt.Printf("Generated binary path: %s\n", file)
})
}
func GetSourcePath() string {
return filepath.Join("v2ray.com", "core", "main")
}
func TestFile(filename string) string {
return filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "scenarios", "data", filename)
}
func InitializeServerSetOnce(testcase string) error {
if err := InitializeServerServer(testcase); err != nil {
return err
}
if err := InitializeServerClient(testcase); err != nil {
return err
}
return nil
}
func InitializeServerServer(testcase string) error {
return InitializeServer(TestFile(testcase + "_server.json"))
}
func InitializeServerClient(testcase string) error {
return InitializeServer(TestFile(testcase + "_client.json"))
}
func InitializeServer(configFile string) error {
err := BuildV2Ray()
if err != nil {
return err
}
proc := RunV2Ray(configFile)
err = proc.Start()
if err != nil {
return err
}
time.Sleep(time.Second)
runningServers = append(runningServers, proc)
return nil
}
func CloseAllServers() {
log.Info("Closing all servers.")
for _, server := range runningServers {
server.Process.Signal(os.Interrupt)
server.Process.Wait()
}
runningServers = make([]*exec.Cmd, 0, 10)
log.Info("All server closed.")
}