StatusCountsTests.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. @testable import BenchmarkUtils
  17. import GRPC
  18. import XCTest
  19. class StatusCountsTests: XCTestCase {
  20. func testIgnoreOK() {
  21. var statusCounts = StatusCounts()
  22. statusCounts.add(status: .ok)
  23. XCTAssertEqual(statusCounts.counts.count, 0)
  24. }
  25. func testMessageBuilding() {
  26. var statusCounts = StatusCounts()
  27. statusCounts.add(status: .aborted)
  28. statusCounts.add(status: .aborted)
  29. statusCounts.add(status: .alreadyExists)
  30. let counts = statusCounts.counts
  31. XCTAssertEqual(counts.count, 2)
  32. for stat in counts {
  33. switch stat.key {
  34. case GRPCStatus.Code.aborted.rawValue:
  35. XCTAssertEqual(stat.value, 2)
  36. case GRPCStatus.Code.alreadyExists.rawValue:
  37. XCTAssertEqual(stat.value, 1)
  38. default:
  39. XCTFail()
  40. }
  41. }
  42. }
  43. func testMergeEmpty() {
  44. var statusCounts = StatusCounts()
  45. statusCounts.add(status: .aborted)
  46. statusCounts.add(status: .aborted)
  47. statusCounts.add(status: .alreadyExists)
  48. let otherCounts = StatusCounts()
  49. statusCounts.merge(source: otherCounts)
  50. let counts = statusCounts.counts
  51. XCTAssertEqual(counts.count, 2)
  52. for stat in counts {
  53. switch stat.key {
  54. case GRPCStatus.Code.aborted.rawValue:
  55. XCTAssertEqual(stat.value, 2)
  56. case GRPCStatus.Code.alreadyExists.rawValue:
  57. XCTAssertEqual(stat.value, 1)
  58. default:
  59. XCTFail()
  60. }
  61. }
  62. }
  63. func testMergeToEmpty() {
  64. var statusCounts = StatusCounts()
  65. var otherCounts = StatusCounts()
  66. otherCounts.add(status: .aborted)
  67. otherCounts.add(status: .aborted)
  68. otherCounts.add(status: .alreadyExists)
  69. statusCounts.merge(source: otherCounts)
  70. let counts = statusCounts.counts
  71. XCTAssertEqual(counts.count, 2)
  72. for stat in counts {
  73. switch stat.key {
  74. case GRPCStatus.Code.aborted.rawValue:
  75. XCTAssertEqual(stat.value, 2)
  76. case GRPCStatus.Code.alreadyExists.rawValue:
  77. XCTAssertEqual(stat.value, 1)
  78. default:
  79. XCTFail()
  80. }
  81. }
  82. }
  83. func testMerge() {
  84. var statusCounts = StatusCounts()
  85. statusCounts.add(status: .aborted)
  86. statusCounts.add(status: .aborted)
  87. statusCounts.add(status: .alreadyExists)
  88. var otherCounts = StatusCounts()
  89. otherCounts.add(status: .alreadyExists)
  90. otherCounts.add(status: .dataLoss)
  91. statusCounts.merge(source: otherCounts)
  92. let counts = statusCounts.counts
  93. XCTAssertEqual(counts.count, 3)
  94. for stat in counts {
  95. switch stat.key {
  96. case GRPCStatus.Code.aborted.rawValue:
  97. XCTAssertEqual(stat.value, 2)
  98. case GRPCStatus.Code.alreadyExists.rawValue:
  99. XCTAssertEqual(stat.value, 2)
  100. case GRPCStatus.Code.dataLoss.rawValue:
  101. XCTAssertEqual(stat.value, 1)
  102. default:
  103. XCTFail()
  104. }
  105. }
  106. }
  107. }