github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/system/mem/mem_test.go (about) 1 package mem 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/isyscore/isc-gobase/system/common" 8 ) 9 10 func skipIfNotImplementedErr(t *testing.T, err error) { 11 if err == common.ErrNotImplementedError { 12 t.Skip("not implemented") 13 } 14 } 15 16 func TestVirtual_memory(t *testing.T) { 17 v, err := VirtualMemory() 18 skipIfNotImplementedErr(t, err) 19 if err != nil { 20 t.Errorf("error %v", err) 21 } 22 t.Log(v) 23 24 } 25 26 func TestSwap_memory(t *testing.T) { 27 v, err := SwapMemory() 28 skipIfNotImplementedErr(t, err) 29 if err != nil { 30 t.Errorf("error %v", err) 31 } 32 empty := &SwapMemoryStat{} 33 if v == empty { 34 t.Errorf("error %v", v) 35 } 36 37 t.Log(v) 38 } 39 40 func TestVirtualMemoryStat_String(t *testing.T) { 41 v := VirtualMemoryStat{ 42 Total: 10, 43 Available: 20, 44 Used: 30, 45 UsedPercent: 30.1, 46 Free: 40, 47 } 48 e := `{"total":10,"available":20,"used":30,"usedPercent":30.1,"free":40,"active":0,"inactive":0,"wired":0,"laundry":0,"buffers":0,"cached":0,"writeback":0,"dirty":0,"writebacktmp":0,"shared":0,"slab":0,"sreclaimable":0,"sunreclaim":0,"pagetables":0,"swapcached":0,"commitlimit":0,"committedas":0,"hightotal":0,"highfree":0,"lowtotal":0,"lowfree":0,"swaptotal":0,"swapfree":0,"mapped":0,"vmalloctotal":0,"vmallocused":0,"vmallocchunk":0,"hugepagestotal":0,"hugepagesfree":0,"hugepagesize":0}` 49 if e != fmt.Sprintf("%v", v) { 50 t.Errorf("VirtualMemoryStat string is invalid: %v", v) 51 } 52 } 53 54 func TestSwapMemoryStat_String(t *testing.T) { 55 v := SwapMemoryStat{ 56 Total: 10, 57 Used: 30, 58 Free: 40, 59 UsedPercent: 30.1, 60 Sin: 1, 61 Sout: 2, 62 PgIn: 3, 63 PgOut: 4, 64 PgFault: 5, 65 PgMajFault: 6, 66 } 67 e := `{"total":10,"used":30,"free":40,"usedPercent":30.1,"sin":1,"sout":2,"pgin":3,"pgout":4,"pgfault":5,"pgmajfault":6}` 68 if e != fmt.Sprintf("%v", v) { 69 t.Errorf("SwapMemoryStat string is invalid: %v", v) 70 } 71 }