github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/network/priorityqueue/priorityqueue_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2018 Go Ethereum作者 10 //此文件是Go以太坊库的一部分。 11 // 12 //Go-Ethereum库是免费软件:您可以重新分发它和/或修改 13 //根据GNU发布的较低通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊图书馆的发行目的是希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU较低的通用公共许可证,了解更多详细信息。 21 // 22 //你应该收到一份GNU较低级别的公共许可证副本 23 //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。 24 package priorityqueue 25 26 import ( 27 "context" 28 "sync" 29 "testing" 30 ) 31 32 func TestPriorityQueue(t *testing.T) { 33 var results []string 34 wg := sync.WaitGroup{} 35 pq := New(3, 2) 36 wg.Add(1) 37 go pq.Run(context.Background(), func(v interface{}) { 38 results = append(results, v.(string)) 39 wg.Done() 40 }) 41 pq.Push(context.Background(), "2.0", 2) 42 wg.Wait() 43 if results[0] != "2.0" { 44 t.Errorf("expected first result %q, got %q", "2.0", results[0]) 45 } 46 47 Loop: 48 for i, tc := range []struct { 49 priorities []int 50 values []string 51 results []string 52 errors []error 53 }{ 54 { 55 priorities: []int{0}, 56 values: []string{""}, 57 results: []string{""}, 58 }, 59 { 60 priorities: []int{0, 1}, 61 values: []string{"0.0", "1.0"}, 62 results: []string{"1.0", "0.0"}, 63 }, 64 { 65 priorities: []int{1, 0}, 66 values: []string{"1.0", "0.0"}, 67 results: []string{"1.0", "0.0"}, 68 }, 69 { 70 priorities: []int{0, 1, 1}, 71 values: []string{"0.0", "1.0", "1.1"}, 72 results: []string{"1.0", "1.1", "0.0"}, 73 }, 74 { 75 priorities: []int{0, 0, 0}, 76 values: []string{"0.0", "0.0", "0.1"}, 77 errors: []error{nil, nil, errContention}, 78 }, 79 } { 80 var results []string 81 wg := sync.WaitGroup{} 82 pq := New(3, 2) 83 wg.Add(len(tc.values)) 84 for j, value := range tc.values { 85 err := pq.Push(nil, value, tc.priorities[j]) 86 if tc.errors != nil && err != tc.errors[j] { 87 t.Errorf("expected push error %v, got %v", tc.errors[j], err) 88 continue Loop 89 } 90 if err != nil { 91 continue Loop 92 } 93 } 94 go pq.Run(context.Background(), func(v interface{}) { 95 results = append(results, v.(string)) 96 wg.Done() 97 }) 98 wg.Wait() 99 for k, result := range tc.results { 100 if results[k] != result { 101 t.Errorf("test case %v: expected %v element %q, got %q", i, k, result, results[k]) 102 } 103 } 104 } 105 }