package server import ( "encoding/json" "os" "os/exec" "strings" "testing" ) func TestProcessIOCounters(t *testing.T) { for _, test := range []struct { data string available bool read, write uint64 }{ {"rchar: 999999\nwchar: 99999\nread_bytes: 1024\nwrite_bytes: 2048\ncancelled_write_bytes: 512\n", true, 1024, 2048}, {"read_bytes: 0\nwrite_bytes: 0\n", true, 0, 0}, {"", false, 0, 0}, {"read_bytes: 100\n", false, 100, 0}, {"read_bytes: -1\nwrite_bytes: 50\n", false, 0, 0}, } { result := parseProcessIO(test.data) if result.Available != test.available || result.ReadBytes != test.read || result.WriteBytes != test.write || result.Scope != "process" || result.ProcessID == "" { t.Fatalf("parse %q: %#v", test.data, result) } } if parseProcessIO("").ProcessID != parseProcessIO("").ProcessID { t.Fatal("unstable process identity") } } func TestResourceIORatesJS(t *testing.T) { node, err := exec.LookPath("node") if err != nil { t.Skip("node is required for JavaScript rate tests") } t.Chdir("../..") script, err := os.ReadFile("static/js/config-panels.js") if err != nil { t.Fatal(err) } code := `const document={addEventListener(){}};` + string(script) + ` const assert=require('node:assert/strict'); const snapshot=(time,read,write,id='process')=>({time,io:{available:true,process_id:id,read_bytes:read,write_bytes:write}}); assert.deepEqual(resourceIORates(snapshot(1000,100,200),snapshot(3000,2148,4296)),{read:1024,write:2048}); assert.deepEqual(resourceIORates(snapshot(1000,100,200),snapshot(2000,100,200)),{read:0,write:0}); for(const [before,after] of [[null,snapshot(1000,1,2)],[{time:0},snapshot(1000,1,2)], [snapshot(1000,1,2),snapshot(2000,3,4,'restarted')],[snapshot(1000,2,3),snapshot(2000,1,4)], [snapshot(1000,1,2),snapshot(1000,3,4)],[snapshot(2000,1,2),snapshot(1000,3,4)], [snapshot(1000,1,2),snapshot(2000,Number.MAX_SAFE_INTEGER+1,4)]]) { const rate=resourceIORates(before,after);assert.ok(Number.isNaN(rate.read)&&Number.isNaN(rate.write)); } ` cmd := exec.Command(node) cmd.Stdin = strings.NewReader(code) if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("I/O rate JS: %v %s", err, out) } } func TestResourceSnapshotIncludesIO(t *testing.T) { s, token, _ := configFixture(t) w := configRequest(s, token, "GET", "/_/api/v1/queue/resources", nil) var snapshot struct { IO processIOCounters `json:"io"` } if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &snapshot) != nil || snapshot.IO.Scope != "process" || snapshot.IO.ProcessID == "" { t.Fatalf("I/O snapshot: %s", w.Body.String()) } }