audio_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2018 The Ebiten 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 audio_test
  15. import (
  16. "bytes"
  17. "runtime"
  18. "testing"
  19. "time"
  20. . "github.com/hajimehoshi/ebiten/v2/audio"
  21. )
  22. var context *Context
  23. func setup() {
  24. context = NewContext(44100)
  25. }
  26. func teardown() {
  27. ResetContextForTesting()
  28. context = nil
  29. }
  30. // Issue #746
  31. func TestGC(t *testing.T) {
  32. setup()
  33. defer teardown()
  34. p, _ := NewPlayer(context, bytes.NewReader(make([]byte, 4)))
  35. got := PlayersNumForTesting()
  36. if want := 0; got != want {
  37. t.Errorf("PlayersNum(): got: %d, want: %d", got, want)
  38. }
  39. p.Play()
  40. got = PlayersNumForTesting()
  41. if want := 1; got != want {
  42. t.Errorf("PlayersNum() after Play: got: %d, want: %d", got, want)
  43. }
  44. runtime.KeepAlive(p)
  45. p = nil
  46. runtime.GC()
  47. for i := 0; i < 10; i++ {
  48. got = PlayersNumForTesting()
  49. if want := 0; got == want {
  50. return
  51. }
  52. if err := UpdateForTesting(); err != nil {
  53. t.Error(err)
  54. }
  55. // 200[ms] should be enough all the bytes are consumed.
  56. // TODO: This is a darty hack. Would it be possible to use virtual time?
  57. time.Sleep(200 * time.Millisecond)
  58. }
  59. t.Errorf("time out")
  60. }
  61. // Issue #853
  62. func TestSameSourcePlayers(t *testing.T) {
  63. setup()
  64. defer teardown()
  65. src := bytes.NewReader(make([]byte, 4))
  66. p0, err := NewPlayer(context, src)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. p1, err := NewPlayer(context, src)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. // As the player does not play yet, error doesn't happen.
  75. if err := UpdateForTesting(); err != nil {
  76. t.Error(err)
  77. }
  78. p0.Play()
  79. p1.Play()
  80. if err := UpdateForTesting(); err == nil {
  81. t.Errorf("got: nil, want: an error")
  82. }
  83. }
  84. func TestPauseBeforeInit(t *testing.T) {
  85. setup()
  86. defer teardown()
  87. src := bytes.NewReader(make([]byte, 4))
  88. p, err := NewPlayer(context, src)
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. p.Play()
  93. p.Pause()
  94. p.Play()
  95. if err := UpdateForTesting(); err != nil {
  96. t.Error(err)
  97. }
  98. }