...

Source file src/github.com/moby/spdystream/priority_test.go

Documentation: github.com/moby/spdystream

     1  /*
     2     Copyright 2014-2021 Docker Inc.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package spdystream
    18  
    19  import (
    20  	"sync"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/moby/spdystream/spdy"
    25  )
    26  
    27  func TestPriorityQueueOrdering(t *testing.T) {
    28  	queue := NewPriorityFrameQueue(150)
    29  	data1 := &spdy.DataFrame{}
    30  	data2 := &spdy.DataFrame{}
    31  	data3 := &spdy.DataFrame{}
    32  	data4 := &spdy.DataFrame{}
    33  	queue.Push(data1, 2)
    34  	queue.Push(data2, 1)
    35  	queue.Push(data3, 1)
    36  	queue.Push(data4, 0)
    37  
    38  	if queue.Pop() != data4 {
    39  		t.Fatalf("Wrong order, expected data4 first")
    40  	}
    41  	if queue.Pop() != data2 {
    42  		t.Fatalf("Wrong order, expected data2 second")
    43  	}
    44  	if queue.Pop() != data3 {
    45  		t.Fatalf("Wrong order, expected data3 third")
    46  	}
    47  	if queue.Pop() != data1 {
    48  		t.Fatalf("Wrong order, expected data1 fourth")
    49  	}
    50  
    51  	// Insert 50 Medium priority frames
    52  	for i := spdy.StreamId(50); i < 100; i++ {
    53  		queue.Push(&spdy.DataFrame{StreamId: i}, 1)
    54  	}
    55  	// Insert 50 low priority frames
    56  	for i := spdy.StreamId(100); i < 150; i++ {
    57  		queue.Push(&spdy.DataFrame{StreamId: i}, 2)
    58  	}
    59  	// Insert 50 high priority frames
    60  	for i := spdy.StreamId(0); i < 50; i++ {
    61  		queue.Push(&spdy.DataFrame{StreamId: i}, 0)
    62  	}
    63  
    64  	for i := spdy.StreamId(0); i < 150; i++ {
    65  		frame := queue.Pop()
    66  		if frame.(*spdy.DataFrame).StreamId != i {
    67  			t.Fatalf("Wrong frame\nActual: %d\nExpecting: %d", frame.(*spdy.DataFrame).StreamId, i)
    68  		}
    69  	}
    70  }
    71  
    72  func TestPriorityQueueSync(t *testing.T) {
    73  	queue := NewPriorityFrameQueue(150)
    74  	var wg sync.WaitGroup
    75  	insertRange := func(start, stop spdy.StreamId, priority uint8) {
    76  		for i := start; i < stop; i++ {
    77  			queue.Push(&spdy.DataFrame{StreamId: i}, priority)
    78  		}
    79  		wg.Done()
    80  	}
    81  	wg.Add(3)
    82  	go insertRange(spdy.StreamId(100), spdy.StreamId(150), 2)
    83  	go insertRange(spdy.StreamId(0), spdy.StreamId(50), 0)
    84  	go insertRange(spdy.StreamId(50), spdy.StreamId(100), 1)
    85  
    86  	wg.Wait()
    87  	for i := spdy.StreamId(0); i < 150; i++ {
    88  		frame := queue.Pop()
    89  		if frame.(*spdy.DataFrame).StreamId != i {
    90  			t.Fatalf("Wrong frame\nActual: %d\nExpecting: %d", frame.(*spdy.DataFrame).StreamId, i)
    91  		}
    92  	}
    93  }
    94  
    95  func TestPriorityQueueBlocking(t *testing.T) {
    96  	queue := NewPriorityFrameQueue(15)
    97  	for i := 0; i < 15; i++ {
    98  		queue.Push(&spdy.DataFrame{}, 2)
    99  	}
   100  	doneChan := make(chan bool)
   101  	go func() {
   102  		queue.Push(&spdy.DataFrame{}, 2)
   103  		close(doneChan)
   104  	}()
   105  	select {
   106  	case <-doneChan:
   107  		t.Fatalf("Push succeeded, expected to block")
   108  	case <-time.After(time.Millisecond):
   109  		break
   110  	}
   111  
   112  	queue.Pop()
   113  
   114  	select {
   115  	case <-doneChan:
   116  		break
   117  	case <-time.After(time.Millisecond):
   118  		t.Fatalf("Push should have succeeded, but timeout reached")
   119  	}
   120  
   121  	for i := 0; i < 15; i++ {
   122  		queue.Pop()
   123  	}
   124  }
   125  

View as plain text