You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.2 KiB

  1. /**
  2. * Standalone signaling server for the Nextcloud Spreed app.
  3. * Copyright (C) 2017 struktur AG
  4. *
  5. * @author Joachim Bauch <bauch@struktur.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package signaling
  23. import (
  24. "strconv"
  25. "sync"
  26. "testing"
  27. )
  28. func TestConcurrentStringStringMap(t *testing.T) {
  29. var m ConcurrentStringStringMap
  30. if m.Len() != 0 {
  31. t.Errorf("Expected %d entries, got %d", 0, m.Len())
  32. }
  33. if v, found := m.Get("foo"); found {
  34. t.Errorf("Expected missing entry, got %s", v)
  35. }
  36. m.Set("foo", "bar")
  37. if m.Len() != 1 {
  38. t.Errorf("Expected %d entries, got %d", 1, m.Len())
  39. }
  40. if v, found := m.Get("foo"); !found {
  41. t.Errorf("Expected entry")
  42. } else if v != "bar" {
  43. t.Errorf("Expected bar, got %s", v)
  44. }
  45. m.Set("foo", "baz")
  46. if m.Len() != 1 {
  47. t.Errorf("Expected %d entries, got %d", 1, m.Len())
  48. }
  49. if v, found := m.Get("foo"); !found {
  50. t.Errorf("Expected entry")
  51. } else if v != "baz" {
  52. t.Errorf("Expected baz, got %s", v)
  53. }
  54. m.Set("lala", "lolo")
  55. if m.Len() != 2 {
  56. t.Errorf("Expected %d entries, got %d", 2, m.Len())
  57. }
  58. if v, found := m.Get("lala"); !found {
  59. t.Errorf("Expected entry")
  60. } else if v != "lolo" {
  61. t.Errorf("Expected lolo, got %s", v)
  62. }
  63. // Deleting missing entries doesn't do anything.
  64. m.Del("xyz")
  65. if m.Len() != 2 {
  66. t.Errorf("Expected %d entries, got %d", 2, m.Len())
  67. }
  68. if v, found := m.Get("foo"); !found {
  69. t.Errorf("Expected entry")
  70. } else if v != "baz" {
  71. t.Errorf("Expected baz, got %s", v)
  72. }
  73. if v, found := m.Get("lala"); !found {
  74. t.Errorf("Expected entry")
  75. } else if v != "lolo" {
  76. t.Errorf("Expected lolo, got %s", v)
  77. }
  78. m.Del("lala")
  79. if m.Len() != 1 {
  80. t.Errorf("Expected %d entries, got %d", 2, m.Len())
  81. }
  82. if v, found := m.Get("foo"); !found {
  83. t.Errorf("Expected entry")
  84. } else if v != "baz" {
  85. t.Errorf("Expected baz, got %s", v)
  86. }
  87. if v, found := m.Get("lala"); found {
  88. t.Errorf("Expected missing entry, got %s", v)
  89. }
  90. m.Clear()
  91. var wg sync.WaitGroup
  92. concurrency := 100
  93. count := 1000
  94. for x := 0; x < concurrency; x++ {
  95. wg.Add(1)
  96. go func(x int) {
  97. defer wg.Done()
  98. key := "key-" + strconv.Itoa(x)
  99. for y := 0; y < count; y = y + 1 {
  100. value := newRandomString(32)
  101. m.Set(key, value)
  102. if v, found := m.Get(key); !found {
  103. t.Errorf("Expected entry for key %s", key)
  104. return
  105. } else if v != value {
  106. t.Errorf("Expected value %s for key %s, got %s", value, key, v)
  107. return
  108. }
  109. }
  110. }(x)
  111. }
  112. wg.Wait()
  113. if m.Len() != concurrency {
  114. t.Errorf("Expected %d entries, got %d", concurrency, m.Len())
  115. }
  116. }