Test data race fix.
Vladimir Bauer
8 years ago
| 41 | 41 | |
| 42 | 42 | type step struct { |
| 43 | 43 | stat *decor.Statistics |
| 44 | dfn decor.DecoratorFunc | |
| 44 | 45 | want string |
| 45 | 46 | } |
| 46 | 47 | |
| 48 | 49 | |
| 49 | 50 | testCases := [][]step{ |
| 50 | 51 | []step{ |
| 51 | {&decor.Statistics{Total: 100, Current: 8}, "8 %"}, | |
| 52 | {&decor.Statistics{Total: 100, Current: 9}, "9 %"}, | |
| 53 | }, | |
| 54 | []step{ | |
| 55 | {&decor.Statistics{Total: 100, Current: 9}, " 9 %"}, | |
| 56 | {&decor.Statistics{Total: 100, Current: 10}, "10 %"}, | |
| 57 | }, | |
| 58 | []step{ | |
| 59 | {&decor.Statistics{Total: 100, Current: 9}, " 9 %"}, | |
| 60 | {&decor.Statistics{Total: 100, Current: 100}, "100 %"}, | |
| 61 | }, | |
| 62 | } | |
| 63 | ||
| 64 | dfn := decor.Percentage(3, decor.DwidthSync) | |
| 65 | testDecoratorConcurrently(t, dfn, testCases) | |
| 52 | { | |
| 53 | &decor.Statistics{Total: 100, Current: 8}, | |
| 54 | decor.Percentage(0, decor.DwidthSync), | |
| 55 | "8 %", | |
| 56 | }, | |
| 57 | { | |
| 58 | &decor.Statistics{Total: 100, Current: 9}, | |
| 59 | decor.Percentage(0, decor.DwidthSync), | |
| 60 | "9 %", | |
| 61 | }, | |
| 62 | }, | |
| 63 | []step{ | |
| 64 | { | |
| 65 | &decor.Statistics{Total: 100, Current: 9}, | |
| 66 | decor.Percentage(0, decor.DwidthSync), | |
| 67 | " 9 %", | |
| 68 | }, | |
| 69 | { | |
| 70 | &decor.Statistics{Total: 100, Current: 10}, | |
| 71 | decor.Percentage(0, decor.DwidthSync), | |
| 72 | "10 %", | |
| 73 | }, | |
| 74 | }, | |
| 75 | []step{ | |
| 76 | { | |
| 77 | &decor.Statistics{Total: 100, Current: 9}, | |
| 78 | decor.Percentage(0, decor.DwidthSync), | |
| 79 | " 9 %", | |
| 80 | }, | |
| 81 | { | |
| 82 | &decor.Statistics{Total: 100, Current: 100}, | |
| 83 | decor.Percentage(0, decor.DwidthSync), | |
| 84 | "100 %", | |
| 85 | }, | |
| 86 | }, | |
| 87 | } | |
| 88 | ||
| 89 | testDecoratorConcurrently(t, testCases) | |
| 66 | 90 | } |
| 67 | 91 | |
| 68 | 92 | func TestPercentageDwidthSyncDidentRight(t *testing.T) { |
| 69 | 93 | |
| 70 | 94 | testCases := [][]step{ |
| 71 | 95 | []step{ |
| 72 | {&decor.Statistics{Total: 100, Current: 8}, "8 %"}, | |
| 73 | {&decor.Statistics{Total: 100, Current: 9}, "9 %"}, | |
| 74 | }, | |
| 75 | []step{ | |
| 76 | {&decor.Statistics{Total: 100, Current: 9}, "9 % "}, | |
| 77 | {&decor.Statistics{Total: 100, Current: 10}, "10 %"}, | |
| 78 | }, | |
| 79 | []step{ | |
| 80 | {&decor.Statistics{Total: 100, Current: 9}, "9 % "}, | |
| 81 | {&decor.Statistics{Total: 100, Current: 100}, "100 %"}, | |
| 82 | }, | |
| 83 | } | |
| 84 | ||
| 85 | dfn := decor.Percentage(3, decor.DwidthSync|decor.DidentRight) | |
| 86 | testDecoratorConcurrently(t, dfn, testCases) | |
| 96 | { | |
| 97 | &decor.Statistics{Total: 100, Current: 8}, | |
| 98 | decor.Percentage(3, decor.DwidthSync|decor.DidentRight), | |
| 99 | "8 %", | |
| 100 | }, | |
| 101 | { | |
| 102 | &decor.Statistics{Total: 100, Current: 9}, | |
| 103 | decor.Percentage(3, decor.DwidthSync|decor.DidentRight), | |
| 104 | "9 %", | |
| 105 | }, | |
| 106 | }, | |
| 107 | []step{ | |
| 108 | { | |
| 109 | &decor.Statistics{Total: 100, Current: 9}, | |
| 110 | decor.Percentage(3, decor.DwidthSync|decor.DidentRight), | |
| 111 | "9 % ", | |
| 112 | }, | |
| 113 | { | |
| 114 | &decor.Statistics{Total: 100, Current: 10}, | |
| 115 | decor.Percentage(3, decor.DwidthSync|decor.DidentRight), | |
| 116 | "10 %", | |
| 117 | }, | |
| 118 | }, | |
| 119 | []step{ | |
| 120 | { | |
| 121 | &decor.Statistics{Total: 100, Current: 9}, | |
| 122 | decor.Percentage(3, decor.DwidthSync|decor.DidentRight), | |
| 123 | "9 % ", | |
| 124 | }, | |
| 125 | { | |
| 126 | &decor.Statistics{Total: 100, Current: 100}, | |
| 127 | decor.Percentage(3, decor.DwidthSync|decor.DidentRight), | |
| 128 | "100 %", | |
| 129 | }, | |
| 130 | }, | |
| 131 | } | |
| 132 | ||
| 133 | testDecoratorConcurrently(t, testCases) | |
| 87 | 134 | } |
| 88 | 135 | |
| 89 | 136 | func TestPercentageDSyncSpace(t *testing.T) { |
| 90 | 137 | |
| 91 | 138 | testCases := [][]step{ |
| 92 | 139 | []step{ |
| 93 | {&decor.Statistics{Total: 100, Current: 8}, " 8 %"}, | |
| 94 | {&decor.Statistics{Total: 100, Current: 9}, " 9 %"}, | |
| 95 | }, | |
| 96 | []step{ | |
| 97 | {&decor.Statistics{Total: 100, Current: 9}, " 9 %"}, | |
| 98 | {&decor.Statistics{Total: 100, Current: 10}, " 10 %"}, | |
| 99 | }, | |
| 100 | []step{ | |
| 101 | {&decor.Statistics{Total: 100, Current: 9}, " 9 %"}, | |
| 102 | {&decor.Statistics{Total: 100, Current: 100}, " 100 %"}, | |
| 103 | }, | |
| 104 | } | |
| 105 | ||
| 106 | dfn := decor.Percentage(3, decor.DSyncSpace) | |
| 107 | testDecoratorConcurrently(t, dfn, testCases) | |
| 108 | } | |
| 109 | ||
| 110 | func testDecoratorConcurrently(t *testing.T, dfn decor.DecoratorFunc, testCases [][]step) { | |
| 140 | { | |
| 141 | &decor.Statistics{Total: 100, Current: 8}, | |
| 142 | decor.Percentage(3, decor.DSyncSpace), | |
| 143 | " 8 %", | |
| 144 | }, | |
| 145 | { | |
| 146 | &decor.Statistics{Total: 100, Current: 9}, | |
| 147 | decor.Percentage(3, decor.DSyncSpace), | |
| 148 | " 9 %", | |
| 149 | }, | |
| 150 | }, | |
| 151 | []step{ | |
| 152 | { | |
| 153 | &decor.Statistics{Total: 100, Current: 9}, | |
| 154 | decor.Percentage(3, decor.DSyncSpace), | |
| 155 | " 9 %", | |
| 156 | }, | |
| 157 | { | |
| 158 | &decor.Statistics{Total: 100, Current: 10}, | |
| 159 | decor.Percentage(3, decor.DSyncSpace), | |
| 160 | " 10 %", | |
| 161 | }, | |
| 162 | }, | |
| 163 | []step{ | |
| 164 | { | |
| 165 | &decor.Statistics{Total: 100, Current: 9}, | |
| 166 | decor.Percentage(3, decor.DSyncSpace), | |
| 167 | " 9 %", | |
| 168 | }, | |
| 169 | { | |
| 170 | &decor.Statistics{Total: 100, Current: 100}, | |
| 171 | decor.Percentage(3, decor.DSyncSpace), | |
| 172 | " 100 %", | |
| 173 | }, | |
| 174 | }, | |
| 175 | } | |
| 176 | ||
| 177 | testDecoratorConcurrently(t, testCases) | |
| 178 | } | |
| 179 | ||
| 180 | func testDecoratorConcurrently(t *testing.T, testCases [][]step) { | |
| 111 | 181 | if len(testCases) == 0 { |
| 112 | 182 | t.Fail() |
| 113 | 183 | } |
| 126 | 196 | res[i] = make(chan string, 1) |
| 127 | 197 | go func(s step, ch chan string) { |
| 128 | 198 | defer wg.Done() |
| 129 | ch <- dfn(s.stat, ws.Accumulator[0], ws.Distributor[0]) | |
| 199 | ch <- s.dfn(s.stat, ws.Accumulator[0], ws.Distributor[0]) | |
| 130 | 200 | }(columnCase[i], res[i]) |
| 131 | 201 | } |
| 132 | 202 | wg.Wait() |