fake_sigar.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package fakes
  2. import (
  3. "time"
  4. sigar "notabug.org/themusicgod1/gosigar"
  5. )
  6. type FakeSigar struct {
  7. LoadAverage sigar.LoadAverage
  8. LoadAverageErr error
  9. Mem sigar.Mem
  10. MemErr error
  11. Swap sigar.Swap
  12. SwapErr error
  13. FileSystemUsage sigar.FileSystemUsage
  14. FileSystemUsageErr error
  15. FileSystemUsagePath string
  16. CollectCpuStatsCpuCh chan sigar.Cpu
  17. CollectCpuStatsStopCh chan struct{}
  18. }
  19. func NewFakeSigar() *FakeSigar {
  20. return &FakeSigar{
  21. CollectCpuStatsCpuCh: make(chan sigar.Cpu, 1),
  22. CollectCpuStatsStopCh: make(chan struct{}),
  23. }
  24. }
  25. func (f *FakeSigar) CollectCpuStats(collectionInterval time.Duration) (<-chan sigar.Cpu, chan<- struct{}) {
  26. samplesCh := make(chan sigar.Cpu, 1)
  27. stopCh := make(chan struct{})
  28. go func() {
  29. for {
  30. select {
  31. case cpuStat := <-f.CollectCpuStatsCpuCh:
  32. select {
  33. case samplesCh <- cpuStat:
  34. default:
  35. // Include default to avoid channel blocking
  36. }
  37. case <-f.CollectCpuStatsStopCh:
  38. return
  39. }
  40. }
  41. }()
  42. return samplesCh, stopCh
  43. }
  44. func (f *FakeSigar) GetLoadAverage() (sigar.LoadAverage, error) {
  45. return f.LoadAverage, f.LoadAverageErr
  46. }
  47. func (f *FakeSigar) GetMem() (sigar.Mem, error) {
  48. return f.Mem, f.MemErr
  49. }
  50. func (f *FakeSigar) GetSwap() (sigar.Swap, error) {
  51. return f.Swap, f.SwapErr
  52. }
  53. func (f *FakeSigar) GetFileSystemUsage(path string) (sigar.FileSystemUsage, error) {
  54. f.FileSystemUsagePath = path
  55. return f.FileSystemUsage, f.FileSystemUsageErr
  56. }