| 1 | package webread |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "runtime" |
| 9 | "testing" |
| 10 | |
| 11 | "golang.org/x/sys/unix" |
| 12 | ) |
| 13 | |
| 14 | type limitsReport struct{ Data, AS, CPU, Core uint64 } |
| 15 | |
| 16 | // limitsMain runs in a child of the test binary: it limits itself as the |
| 17 | // fetcher does, then shows the limits hold and that Go still runs. |
| 18 | func limitsMain(over bool) int { |
| 19 | if err := limitSelf(); err != nil { |
| 20 | fmt.Fprintln(os.Stderr, err) |
| 21 | return 1 |
| 22 | } |
| 23 | size := 128 << 20 // a decoded 16-megapixel image, twice over |
| 24 | if over { |
| 25 | size = 1200 << 20 |
| 26 | } |
| 27 | buf := make([]byte, size) |
| 28 | buf[len(buf)-1] = 1 |
| 29 | var r limitsReport |
| 30 | var l unix.Rlimit |
| 31 | unix.Getrlimit(unix.RLIMIT_DATA, &l) |
| 32 | r.Data = l.Max |
| 33 | unix.Getrlimit(unix.RLIMIT_AS, &l) |
| 34 | r.AS = l.Max |
| 35 | unix.Getrlimit(unix.RLIMIT_CPU, &l) |
| 36 | r.CPU = l.Max |
| 37 | unix.Getrlimit(unix.RLIMIT_CORE, &l) |
| 38 | r.Core = l.Max |
| 39 | json.NewEncoder(os.Stdout).Encode(r) |
| 40 | return 0 |
| 41 | } |
| 42 | |
| 43 | func TestFetcherLimitsItself(t *testing.T) { |
| 44 | if runtime.GOOS != "linux" { |
| 45 | t.Skip("the fetcher runs only on Linux") |
| 46 | } |
| 47 | self, err := os.Executable() |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | out, err := exec.Command(self, "webread-limits").Output() |
| 52 | if err != nil { |
| 53 | t.Fatalf("child: %v", err) |
| 54 | } |
| 55 | var r limitsReport |
| 56 | if err = json.Unmarshal(out, &r); err != nil { |
| 57 | t.Fatalf("%v: %s", err, out) |
| 58 | } |
| 59 | if r.Data != 1<<30 || r.AS != 4<<30 || r.CPU != 120 || r.Core != 0 { |
| 60 | t.Fatalf("limits %+v", r) |
| 61 | } |
| 62 | // The data bound holds: more than 1 GB cannot be had. |
| 63 | if out, err := exec.Command(self, "webread-limits-over").CombinedOutput(); err == nil { |
| 64 | t.Fatalf("allocated past the bound: %s", out) |
| 65 | } |
| 66 | } |
| 67 | |