func startServer() { log.Printf("cpumonitor listening on %d \n", *port) cpuCalculator := new(stats.CPUOps) runInterval := time.Duration(int32(*runInterval)) * time.Millisecond cpuInterval := time.Duration(int32(*cpuInterval)) * time.Second cpuCollector := stats.NewCPUCollector(cpuCalculator, stats.DefaultTicker(), runInterval, cpuInterval, *perCPU) statsHandler := stats.NewStatHandler(cpuCollector) http.HandleFunc("/start", statsHandler.Start) http.HandleFunc("/stop", statsHandler.Stop) err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }
testTickerHarness stats.TickerHarness ) BeforeEach(func() { ch = make(chan time.Time) testTickerHarness = func(d time.Duration) <-chan time.Time { return ch } }) Describe("Run", func() { It("returns an error if cpu percent errors", func() { fakeCpu := new(fakes.FakeCpu) expectedErr := errors.New("something bad happened") fakeCpu.PercentReturns(nil, expectedErr) collector := stats.NewCPUCollector(fakeCpu, stats.DefaultTicker(), time.Second, time.Second, false) Eventually(func() error { return collector.Run() }()).Should(Equal(expectedErr)) }) It("uses specified cpu arguments", func() { fakeCpu := new(fakes.FakeCpu) cpuInterval := time.Second perCPU := false collector := stats.NewCPUCollector(fakeCpu, testTickerHarness, time.Second, cpuInterval, perCPU) go collector.Run() ch <- time.Time{} interval, perCpu := fakeCpu.PercentArgsForCall(0) Expect(interval).To(Equal(cpuInterval))