pool_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  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 util
  15. import (
  16. "sync"
  17. "testing"
  18. )
  19. func TestDriverPool(t *testing.T) {
  20. url := "mock"
  21. testPool := newDBPool()
  22. testPool.isTesting = true
  23. expCount := 3
  24. wg := sync.WaitGroup{}
  25. wg.Add(expCount)
  26. for i := 0; i < expCount; i++ {
  27. go func() {
  28. defer func() {
  29. wg.Done()
  30. }()
  31. _, err := FetchDBToOneNode(testPool, url)
  32. if err != nil {
  33. t.Errorf("meet unexpected err:%v", err)
  34. }
  35. }()
  36. }
  37. wg.Wait()
  38. count := getDBConnCount(testPool, url)
  39. if expCount != count {
  40. t.Errorf("expect conn count:%v, got:%v", expCount, count)
  41. }
  42. wg.Add(expCount)
  43. for i := 0; i < expCount; i++ {
  44. go func() {
  45. defer func() {
  46. wg.Done()
  47. }()
  48. err := ReturnDBFromOneNode(testPool, url)
  49. if err != nil {
  50. t.Errorf("meet unexpected err:%v", err)
  51. }
  52. }()
  53. }
  54. wg.Wait()
  55. count = getDBConnCount(testPool, url)
  56. if count != 0 {
  57. t.Errorf("expect conn count:%v, got:%v", 0, count)
  58. }
  59. }