recorder.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2015 The etcd 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 testutil
  15. import (
  16. "errors"
  17. "fmt"
  18. "sync"
  19. "time"
  20. )
  21. type Action struct {
  22. Name string
  23. Params []interface{}
  24. }
  25. type Recorder interface {
  26. // Record publishes an Action (e.g., function call) which will
  27. // be reflected by Wait() or Chan()
  28. Record(a Action)
  29. // Wait waits until at least n Actions are available or returns with error
  30. Wait(n int) ([]Action, error)
  31. // Action returns immediately available Actions
  32. Action() []Action
  33. // Chan returns the channel for actions published by Record
  34. Chan() <-chan Action
  35. }
  36. // RecorderBuffered appends all Actions to a slice
  37. type RecorderBuffered struct {
  38. sync.Mutex
  39. actions []Action
  40. }
  41. func (r *RecorderBuffered) Record(a Action) {
  42. r.Lock()
  43. r.actions = append(r.actions, a)
  44. r.Unlock()
  45. }
  46. func (r *RecorderBuffered) Action() []Action {
  47. r.Lock()
  48. cpy := make([]Action, len(r.actions))
  49. copy(cpy, r.actions)
  50. r.Unlock()
  51. return cpy
  52. }
  53. func (r *RecorderBuffered) Wait(n int) (acts []Action, err error) {
  54. // legacy racey behavior
  55. WaitSchedule()
  56. acts = r.Action()
  57. if len(acts) < n {
  58. err = newLenErr(n, len(acts))
  59. }
  60. return acts, err
  61. }
  62. func (r *RecorderBuffered) Chan() <-chan Action {
  63. ch := make(chan Action)
  64. go func() {
  65. acts := r.Action()
  66. for i := range acts {
  67. ch <- acts[i]
  68. }
  69. close(ch)
  70. }()
  71. return ch
  72. }
  73. // RecorderStream writes all Actions to an unbuffered channel
  74. type recorderStream struct {
  75. ch chan Action
  76. }
  77. func NewRecorderStream() Recorder {
  78. return &recorderStream{ch: make(chan Action)}
  79. }
  80. func (r *recorderStream) Record(a Action) {
  81. r.ch <- a
  82. }
  83. func (r *recorderStream) Action() (acts []Action) {
  84. for {
  85. select {
  86. case act := <-r.ch:
  87. acts = append(acts, act)
  88. default:
  89. return acts
  90. }
  91. }
  92. }
  93. func (r *recorderStream) Chan() <-chan Action {
  94. return r.ch
  95. }
  96. func (r *recorderStream) Wait(n int) ([]Action, error) {
  97. acts := make([]Action, n)
  98. timeoutC := time.After(5 * time.Second)
  99. for i := 0; i < n; i++ {
  100. select {
  101. case acts[i] = <-r.ch:
  102. case <-timeoutC:
  103. acts = acts[:i]
  104. return acts, newLenErr(n, i)
  105. }
  106. }
  107. // extra wait to catch any Action spew
  108. select {
  109. case act := <-r.ch:
  110. acts = append(acts, act)
  111. case <-time.After(10 * time.Millisecond):
  112. }
  113. return acts, nil
  114. }
  115. func newLenErr(expected int, actual int) error {
  116. s := fmt.Sprintf("len(actions) = %d, expected >= %d", actual, expected)
  117. return errors.New(s)
  118. }