TestWantConnQueue_len_Concurrent tests len() thread safety.
(t *testing.T)
| 873 | |
| 874 | // TestWantConnQueue_len_Concurrent tests len() thread safety. |
| 875 | func TestWantConnQueue_len_Concurrent(t *testing.T) { |
| 876 | q := newWantConnQueue() |
| 877 | const numReaders = 10 |
| 878 | const numWriters = 5 |
| 879 | const operations = 100 |
| 880 | |
| 881 | var wg sync.WaitGroup |
| 882 | |
| 883 | // Multiple readers calling len() |
| 884 | for i := 0; i < numReaders; i++ { |
| 885 | wg.Add(1) |
| 886 | go func() { |
| 887 | defer wg.Done() |
| 888 | for j := 0; j < operations; j++ { |
| 889 | _ = q.len() // Just read, don't care about value |
| 890 | time.Sleep(time.Microsecond) |
| 891 | } |
| 892 | }() |
| 893 | } |
| 894 | |
| 895 | // Writers enqueueing |
| 896 | for i := 0; i < numWriters; i++ { |
| 897 | wg.Add(1) |
| 898 | go func() { |
| 899 | defer wg.Done() |
| 900 | for j := 0; j < operations; j++ { |
| 901 | w := &wantConn{ |
| 902 | ctx: context.Background(), |
| 903 | result: make(chan wantConnResult, 1), |
| 904 | } |
| 905 | q.enqueue(w) |
| 906 | } |
| 907 | }() |
| 908 | } |
| 909 | |
| 910 | wg.Wait() |
| 911 | |
| 912 | // Verify final length is correct |
| 913 | expectedLength := numWriters * operations |
| 914 | if length := q.len(); length != expectedLength { |
| 915 | t.Errorf("final queue len() = %d, want %d", length, expectedLength) |
| 916 | } |
| 917 | } |
nothing calls this directly
no test coverage detected