@cryptotaxi247 / kubo / commits / 7639a2f0a

test: introduce go-timeout

License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Aug 31, 2016 at 19:45 UTC 7639a2f0a7e1a54d00fb1bd9f902cb1deab63d38
3 files changed +78
test/dependencies/go-timeout/.gitignore new
+1
@@ -0,0 +1 @@
1 +go-timeout
test/dependencies/go-timeout/LICENSE new
+22
@@ -0,0 +1,22 @@
1 +The MIT License (MIT)
2 +
3 +Copyright (c) 2016 Jakub "Kubuxu" Sztandera <k.sztandera@protonmail.ch>
4 +
5 +Permission is hereby granted, free of charge, to any person obtaining a copy
6 +of this software and associated documentation files (the "Software"), to deal
7 +in the Software without restriction, including without limitation the rights
8 +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 +copies of the Software, and to permit persons to whom the Software is
10 +furnished to do so, subject to the following conditions:
11 +
12 +The above copyright notice and this permission notice shall be included in
13 +all copies or substantial portions of the Software.
14 +
15 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 +THE SOFTWARE.
22 +
test/dependencies/go-timeout/main.go new
+55
@@ -0,0 +1,55 @@
1 +package main
2 +
3 +import (
4 + "context"
5 + "fmt"
6 + "os"
7 + "os/exec"
8 + "strconv"
9 + "syscall"
10 + "time"
11 +)
12 +
13 +func main() {
14 + if len(os.Args) < 3 {
15 + fmt.Fprintf(os.Stderr,
16 + "Usage: %s <timeout-in-sec> <command ...>\n", os.Args[0])
17 + os.Exit(1)
18 + }
19 + timeout, err := strconv.ParseUint(os.Args[1], 10, 32)
20 + if err != nil {
21 + fmt.Fprintf(os.Stderr, "Error: %v\n", err)
22 + os.Exit(1)
23 + }
24 + ctx, _ := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
25 +
26 + cmd := exec.CommandContext(ctx, os.Args[2], os.Args[3:]...)
27 + cmd.Stdin = os.Stdin
28 + cmd.Stdout = os.Stdout
29 + cmd.Stderr = os.Stderr
30 + err = cmd.Start()
31 + if err != nil {
32 + fmt.Fprintf(os.Stderr, "Error: %v\n", err)
33 + }
34 + err = cmd.Wait()
35 +
36 + if err != nil {
37 + if ctx.Err() != nil {
38 + os.Exit(124)
39 + } else {
40 + exitErr, ok := err.(*exec.ExitError)
41 + if !ok {
42 + fmt.Fprintf(os.Stderr, "Error: %v\n", err)
43 + os.Exit(255)
44 + }
45 + waits, ok := exitErr.Sys().(syscall.WaitStatus)
46 + if !ok {
47 + fmt.Fprintf(os.Stderr, "Error: %v\n", err)
48 + os.Exit(255)
49 + }
50 + os.Exit(waits.ExitStatus())
51 + }
52 + } else {
53 + os.Exit(0)
54 + }
55 +}