25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

100 lines
3.4 KiB

  1. // Copyright 2018, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package zpages
  15. import (
  16. "bytes"
  17. "html/template"
  18. "testing"
  19. )
  20. const tmplBody = `
  21. <td><b>{{.Method}}</b></td>
  22. <td></td>
  23. <td align="right">{{.CountMinute|count}}</td>
  24. <td align="right">{{.CountHour|count}}</td>
  25. <td align="right">{{.CountTotal|count}}</td><td></td>
  26. <td align="right">{{.AvgLatencyMinute|ms}}</td>
  27. <td align="right">{{.AvgLatencyHour|ms}}</td>
  28. <td align="right">{{.AvgLatencyTotal|ms}}</td><td></td>
  29. <td align="right">{{.RPCRateMinute|rate}}</td>
  30. <td align="right">{{.RPCRateHour|rate}}</td>
  31. <td align="right">{{.RPCRateTotal|rate}}</td><td></td>
  32. <td align="right">{{.InputRateMinute|datarate}}</td>
  33. <td align="right">{{.InputRateHour|datarate}}</td>
  34. <td align="right">{{.InputRateTotal|datarate}}</td><td></td>
  35. <td align="right">{{.OutputRateMinute|datarate}}</td>
  36. <td align="right">{{.OutputRateHour|datarate}}</td>
  37. <td align="right">{{.OutputRateTotal|datarate}}</td><td></td>
  38. <td align="right">{{.ErrorsMinute|count}}</td>
  39. <td align="right">{{.ErrorsHour|count}}</td>
  40. <td align="right">{{.ErrorsTotal|count}}</td><td></td>
  41. `
  42. var tmpl = template.Must(template.New("countTest").Funcs(templateFunctions).Parse(tmplBody))
  43. func TestTemplateFuncs(t *testing.T) {
  44. buf := new(bytes.Buffer)
  45. sshot := &statSnapshot{
  46. Method: "Foo",
  47. CountMinute: 1e9,
  48. CountHour: 5000,
  49. CountTotal: 1e12,
  50. AvgLatencyMinute: 10000,
  51. AvgLatencyHour: 1000,
  52. AvgLatencyTotal: 20000,
  53. RPCRateMinute: 2000,
  54. RPCRateHour: 5000,
  55. RPCRateTotal: 75000,
  56. InputRateMinute: 75000,
  57. InputRateHour: 75000,
  58. InputRateTotal: 75000,
  59. OutputRateMinute: 75000,
  60. OutputRateHour: 75000,
  61. OutputRateTotal: 75000,
  62. ErrorsMinute: 120000000,
  63. ErrorsHour: 75000000,
  64. ErrorsTotal: 7500000,
  65. }
  66. if err := tmpl.Execute(buf, sshot); err != nil {
  67. t.Fatalf("Failed to execute template: %v", err)
  68. }
  69. want := `
  70. <td><b>Foo</b></td>
  71. <td></td>
  72. <td align="right">1.000 G </td>
  73. <td align="right">5000</td>
  74. <td align="right">1.000 T </td><td></td>
  75. <td align="right">0.010</td>
  76. <td align="right">0.001</td>
  77. <td align="right">0.020</td><td></td>
  78. <td align="right">2000.000</td>
  79. <td align="right">5000.000</td>
  80. <td align="right">75000.000</td><td></td>
  81. <td align="right">0.075</td>
  82. <td align="right">0.075</td>
  83. <td align="right">0.075</td><td></td>
  84. <td align="right">0.075</td>
  85. <td align="right">0.075</td>
  86. <td align="right">0.075</td><td></td>
  87. <td align="right">120.000 M </td>
  88. <td align="right">75.000 M </td>
  89. <td align="right">7.500 M </td><td></td>
  90. `
  91. if g, w := buf.String(), want; g != w {
  92. t.Errorf("Output mismatch:\nGot:\n\t%s\nWant:\n\t%s", g, w)
  93. }
  94. }