event_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package grpcsync
  19. import (
  20. "testing"
  21. "google.golang.org/grpc/internal/grpctest"
  22. )
  23. type s struct {
  24. grpctest.Tester
  25. }
  26. func Test(t *testing.T) {
  27. grpctest.RunSubTests(t, s{})
  28. }
  29. func (s) TestEventHasFired(t *testing.T) {
  30. e := NewEvent()
  31. if e.HasFired() {
  32. t.Fatal("e.HasFired() = true; want false")
  33. }
  34. if !e.Fire() {
  35. t.Fatal("e.Fire() = false; want true")
  36. }
  37. if !e.HasFired() {
  38. t.Fatal("e.HasFired() = false; want true")
  39. }
  40. }
  41. func (s) TestEventDoneChannel(t *testing.T) {
  42. e := NewEvent()
  43. select {
  44. case <-e.Done():
  45. t.Fatal("e.HasFired() = true; want false")
  46. default:
  47. }
  48. if !e.Fire() {
  49. t.Fatal("e.Fire() = false; want true")
  50. }
  51. select {
  52. case <-e.Done():
  53. default:
  54. t.Fatal("e.HasFired() = false; want true")
  55. }
  56. }
  57. func (s) TestEventMultipleFires(t *testing.T) {
  58. e := NewEvent()
  59. if e.HasFired() {
  60. t.Fatal("e.HasFired() = true; want false")
  61. }
  62. if !e.Fire() {
  63. t.Fatal("e.Fire() = false; want true")
  64. }
  65. for i := 0; i < 3; i++ {
  66. if !e.HasFired() {
  67. t.Fatal("e.HasFired() = false; want true")
  68. }
  69. if e.Fire() {
  70. t.Fatal("e.Fire() = true; want false")
  71. }
  72. }
  73. }