github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/swarm/network/priorityqueue/priorityqueue_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  package priorityqueue
    17  
    18  import (
    19  	"context"
    20  	"sync"
    21  	"testing"
    22  )
    23  
    24  func TestPriorityQueue(t *testing.T) {
    25  	var results []string
    26  	wg := sync.WaitGroup{}
    27  	pq := New(3, 2)
    28  	wg.Add(1)
    29  	go pq.Run(context.Background(), func(v interface{}) {
    30  		results = append(results, v.(string))
    31  		wg.Done()
    32  	})
    33  	pq.Push("2.0", 2)
    34  	wg.Wait()
    35  	if results[0] != "2.0" {
    36  		t.Errorf("expected first result %q, got %q", "2.0", results[0])
    37  	}
    38  
    39  Loop:
    40  	for i, tc := range []struct {
    41  		priorities []int
    42  		values     []string
    43  		results    []string
    44  		errors     []error
    45  	}{
    46  		{
    47  			priorities: []int{0},
    48  			values:     []string{""},
    49  			results:    []string{""},
    50  		},
    51  		{
    52  			priorities: []int{0, 1},
    53  			values:     []string{"0.0", "1.0"},
    54  			results:    []string{"1.0", "0.0"},
    55  		},
    56  		{
    57  			priorities: []int{1, 0},
    58  			values:     []string{"1.0", "0.0"},
    59  			results:    []string{"1.0", "0.0"},
    60  		},
    61  		{
    62  			priorities: []int{0, 1, 1},
    63  			values:     []string{"0.0", "1.0", "1.1"},
    64  			results:    []string{"1.0", "1.1", "0.0"},
    65  		},
    66  		{
    67  			priorities: []int{0, 0, 0},
    68  			values:     []string{"0.0", "0.0", "0.1"},
    69  			errors:     []error{nil, nil, ErrContention},
    70  		},
    71  	} {
    72  		var results []string
    73  		wg := sync.WaitGroup{}
    74  		pq := New(3, 2)
    75  		wg.Add(len(tc.values))
    76  		for j, value := range tc.values {
    77  			err := pq.Push(value, tc.priorities[j])
    78  			if tc.errors != nil && err != tc.errors[j] {
    79  				t.Errorf("expected push error %v, got %v", tc.errors[j], err)
    80  				continue Loop
    81  			}
    82  			if err != nil {
    83  				continue Loop
    84  			}
    85  		}
    86  		go pq.Run(context.Background(), func(v interface{}) {
    87  			results = append(results, v.(string))
    88  			wg.Done()
    89  		})
    90  		wg.Wait()
    91  		for k, result := range tc.results {
    92  			if results[k] != result {
    93  				t.Errorf("test case %v: expected %v element %q, got %q", i, k, result, results[k])
    94  			}
    95  		}
    96  	}
    97  }