1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 11:35:23 +00:00
v2fly/testing/scenarios/server_env.go

103 lines
1.9 KiB
Go
Raw Normal View History

2015-12-04 15:49:10 +00:00
package scenarios
import (
2015-12-04 15:49:45 +00:00
"os"
2016-06-03 22:38:22 +00:00
"os/exec"
2015-12-04 15:49:45 +00:00
"path/filepath"
2016-06-04 10:41:48 +00:00
"runtime"
2016-06-04 11:27:17 +00:00
"time"
2015-12-04 15:49:10 +00:00
2017-02-01 20:35:40 +00:00
"v2ray.com/core/app/log"
2015-12-04 15:49:10 +00:00
2016-11-17 23:01:37 +00:00
"fmt"
"io/ioutil"
"sync"
2016-11-05 00:00:20 +00:00
_ "v2ray.com/core"
2016-11-17 23:01:37 +00:00
"v2ray.com/core/common/retry"
2015-12-04 15:49:10 +00:00
)
2015-12-04 16:32:42 +00:00
var (
2016-11-17 23:01:37 +00:00
runningServers = make([]*exec.Cmd, 0, 10)
testBinaryPath string
testBinaryPathGen sync.Once
2015-12-04 16:32:42 +00:00
)
2016-11-17 23:01:37 +00:00
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)
})
2016-08-09 20:13:16 +00:00
}
func GetSourcePath() string {
2016-10-12 14:46:02 +00:00
return filepath.Join("v2ray.com", "core", "main")
2016-06-03 22:38:22 +00:00
}
2015-12-04 15:49:10 +00:00
func TestFile(filename string) string {
2016-08-20 19:03:50 +00:00
return filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "scenarios", "data", filename)
2015-12-04 15:49:10 +00:00
}
2015-12-04 16:32:42 +00:00
func InitializeServerSetOnce(testcase string) error {
2016-01-28 12:40:00 +00:00
if err := InitializeServerServer(testcase); err != nil {
2015-12-04 16:32:42 +00:00
return err
}
2016-01-28 12:40:00 +00:00
if err := InitializeServerClient(testcase); err != nil {
2015-12-04 16:32:42 +00:00
return err
}
return nil
}
2016-01-28 12:40:00 +00:00
func InitializeServerServer(testcase string) error {
return InitializeServer(TestFile(testcase + "_server.json"))
}
func InitializeServerClient(testcase string) error {
return InitializeServer(TestFile(testcase + "_client.json"))
}
2015-12-04 15:49:10 +00:00
func InitializeServer(configFile string) error {
2016-06-03 22:38:22 +00:00
err := BuildV2Ray()
2015-12-04 15:49:10 +00:00
if err != nil {
return err
}
2016-08-08 22:55:36 +00:00
proc := RunV2Ray(configFile)
2015-12-04 15:49:10 +00:00
2016-06-03 22:38:22 +00:00
err = proc.Start()
2015-12-04 15:49:10 +00:00
if err != nil {
return err
}
2016-06-03 22:38:22 +00:00
2016-06-04 11:27:17 +00:00
time.Sleep(time.Second)
2016-06-03 22:38:22 +00:00
runningServers = append(runningServers, proc)
2015-12-04 15:49:10 +00:00
return nil
}
2016-01-03 23:33:25 +00:00
func CloseAllServers() {
2016-06-03 22:38:22 +00:00
log.Info("Closing all servers.")
2016-01-03 23:33:25 +00:00
for _, server := range runningServers {
2016-08-09 20:13:16 +00:00
server.Process.Signal(os.Interrupt)
server.Process.Wait()
2016-01-03 23:33:25 +00:00
}
2016-06-03 22:38:22 +00:00
runningServers = make([]*exec.Cmd, 0, 10)
log.Info("All server closed.")
2016-01-03 23:33:25 +00:00
}