github.com/pion/webrtc/v4@v4.0.1/operations_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package webrtc 5 6 import ( 7 "sync" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestOperations_Enqueue(t *testing.T) { 14 updateNegotiationNeededFlagOnEmptyChain := &atomicBool{} 15 onNegotiationNeededCalledCount := 0 16 var onNegotiationNeededCalledCountMu sync.Mutex 17 ops := newOperations(updateNegotiationNeededFlagOnEmptyChain, func() { 18 onNegotiationNeededCalledCountMu.Lock() 19 onNegotiationNeededCalledCount++ 20 onNegotiationNeededCalledCountMu.Unlock() 21 }) 22 defer ops.GracefulClose() 23 24 for resultSet := 0; resultSet < 100; resultSet++ { 25 results := make([]int, 16) 26 resultSetCopy := resultSet 27 for i := range results { 28 func(j int) { 29 ops.Enqueue(func() { 30 results[j] = j * j 31 if resultSetCopy > 50 { 32 updateNegotiationNeededFlagOnEmptyChain.set(true) 33 } 34 }) 35 }(i) 36 } 37 38 ops.Done() 39 expected := []int{0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225} 40 assert.Equal(t, len(expected), len(results)) 41 assert.Equal(t, expected, results) 42 } 43 onNegotiationNeededCalledCountMu.Lock() 44 defer onNegotiationNeededCalledCountMu.Unlock() 45 assert.NotEqual(t, onNegotiationNeededCalledCount, 0) 46 } 47 48 func TestOperations_Done(*testing.T) { 49 ops := newOperations(&atomicBool{}, func() { 50 }) 51 defer ops.GracefulClose() 52 ops.Done() 53 } 54 55 func TestOperations_GracefulClose(t *testing.T) { 56 ops := newOperations(&atomicBool{}, func() { 57 }) 58 59 counter := 0 60 var counterMu sync.Mutex 61 incFunc := func() { 62 counterMu.Lock() 63 counter++ 64 counterMu.Unlock() 65 } 66 const times = 25 67 for i := 0; i < times; i++ { 68 ops.Enqueue(incFunc) 69 } 70 ops.Done() 71 counterMu.Lock() 72 counterCur := counter 73 counterMu.Unlock() 74 assert.Equal(t, counterCur, times) 75 76 ops.GracefulClose() 77 for i := 0; i < times; i++ { 78 ops.Enqueue(incFunc) 79 } 80 ops.Done() 81 assert.Equal(t, counterCur, times) 82 }