github.com/ilhicas/nomad@v0.11.8/drivers/docker/stats_test.go (about) 1 package docker 2 3 import ( 4 "runtime" 5 "sync" 6 "testing" 7 "time" 8 9 docker "github.com/fsouza/go-dockerclient" 10 cstructs "github.com/hashicorp/nomad/client/structs" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestDriver_DockerStatsCollector(t *testing.T) { 15 t.Parallel() 16 17 require := require.New(t) 18 src := make(chan *docker.Stats) 19 defer close(src) 20 dst, recvCh := newStatsChanPipe() 21 defer dst.close() 22 stats := &docker.Stats{} 23 stats.CPUStats.ThrottlingData.Periods = 10 24 stats.CPUStats.ThrottlingData.ThrottledPeriods = 10 25 stats.CPUStats.ThrottlingData.ThrottledTime = 10 26 27 stats.MemoryStats.Stats.Rss = 6537216 28 stats.MemoryStats.Stats.Cache = 1234 29 stats.MemoryStats.Stats.Swap = 0 30 stats.MemoryStats.Usage = 5651904 31 stats.MemoryStats.MaxUsage = 6651904 32 stats.MemoryStats.Commit = 123231 33 stats.MemoryStats.CommitPeak = 321323 34 stats.MemoryStats.PrivateWorkingSet = 62222 35 36 go dockerStatsCollector(dst, src, time.Second) 37 38 select { 39 case src <- stats: 40 case <-time.After(time.Second): 41 require.Fail("sending stats should not block here") 42 } 43 44 select { 45 case ru := <-recvCh: 46 if runtime.GOOS != "windows" { 47 require.Equal(stats.MemoryStats.Stats.Rss, ru.ResourceUsage.MemoryStats.RSS) 48 require.Equal(stats.MemoryStats.Stats.Cache, ru.ResourceUsage.MemoryStats.Cache) 49 require.Equal(stats.MemoryStats.Stats.Swap, ru.ResourceUsage.MemoryStats.Swap) 50 require.Equal(stats.MemoryStats.Usage, ru.ResourceUsage.MemoryStats.Usage) 51 require.Equal(stats.MemoryStats.MaxUsage, ru.ResourceUsage.MemoryStats.MaxUsage) 52 require.Equal(stats.CPUStats.ThrottlingData.ThrottledPeriods, ru.ResourceUsage.CpuStats.ThrottledPeriods) 53 require.Equal(stats.CPUStats.ThrottlingData.ThrottledTime, ru.ResourceUsage.CpuStats.ThrottledTime) 54 } else { 55 require.Equal(stats.MemoryStats.PrivateWorkingSet, ru.ResourceUsage.MemoryStats.RSS) 56 require.Equal(stats.MemoryStats.Commit, ru.ResourceUsage.MemoryStats.Usage) 57 require.Equal(stats.MemoryStats.CommitPeak, ru.ResourceUsage.MemoryStats.MaxUsage) 58 require.Equal(stats.CPUStats.ThrottlingData.ThrottledPeriods, ru.ResourceUsage.CpuStats.ThrottledPeriods) 59 require.Equal(stats.CPUStats.ThrottlingData.ThrottledTime, ru.ResourceUsage.CpuStats.ThrottledTime) 60 61 } 62 case <-time.After(time.Second): 63 require.Fail("receiving stats should not block here") 64 } 65 } 66 67 // TestDriver_DockerUsageSender asserts that the TaskResourceUsage chan wrapper 68 // supports closing and sending on a chan from concurrent goroutines. 69 func TestDriver_DockerUsageSender(t *testing.T) { 70 t.Parallel() 71 72 // sample payload 73 res := &cstructs.TaskResourceUsage{} 74 75 destCh, recvCh := newStatsChanPipe() 76 77 // Sending should never fail 78 destCh.send(res) 79 destCh.send(res) 80 destCh.send(res) 81 82 // Clear chan 83 <-recvCh 84 85 // Send and close concurrently to let the race detector help us out 86 wg := sync.WaitGroup{} 87 wg.Add(3) 88 89 // Sender 90 go func() { 91 destCh.send(res) 92 wg.Done() 93 }() 94 95 // Closer 96 go func() { 97 destCh.close() 98 wg.Done() 99 }() 100 101 // Clear recv chan 102 go func() { 103 for range recvCh { 104 } 105 wg.Done() 106 }() 107 108 wg.Wait() 109 110 // Assert closed 111 destCh.mu.Lock() 112 closed := destCh.closed 113 destCh.mu.Unlock() 114 require.True(t, closed) 115 116 select { 117 case _, ok := <-recvCh: 118 require.False(t, ok) 119 default: 120 require.Fail(t, "expect recvCh to be closed") 121 } 122 123 // Assert sending and closing never fails 124 destCh.send(res) 125 destCh.close() 126 destCh.close() 127 destCh.send(res) 128 }