github.com/turingchain2020/turingchain@v1.1.21/system/p2p/dht/protocol/download/task_test.go (about) 1 package download 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/libp2p/go-libp2p-core/peer" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func testJobs(t *testing.T) { 12 pid1, _ := peer.Decode("16Uiu2HAmGpMpYDDidb27555ALTx7a1aZbqYDa7B2EUwwCiBcL67M") 13 pid2, _ := peer.Decode("16Uiu2HAmTdgKpRmE6sXj512HodxBPMZmjh6vHG1m4ftnXY3wLSpg") 14 pid3, _ := peer.Decode("16Uiu2HAm45QtjUVYxnc3eqfHoE4eSFovSh99SgsoF6Qm1eRXTd5W") 15 16 t1 := &taskInfo{ 17 ID: "123456", 18 TaskNum: 10, 19 Pid: pid1, 20 Index: 0, 21 Latency: time.Second * 10, 22 } 23 t2 := &taskInfo{ 24 ID: "123456", 25 TaskNum: 11, 26 Pid: pid2, 27 Index: 1, 28 Latency: time.Second * 5, 29 } 30 t3 := &taskInfo{ 31 ID: "123456", 32 TaskNum: 14, 33 Pid: pid3, 34 Index: 2, 35 Latency: time.Second * 20, 36 } 37 38 var myjobs tasks 39 require.Equal(t, myjobs.Len(), 0) 40 myjobs = append(myjobs, t1, t2, t3) 41 require.Equal(t, myjobs.Len(), 3) 42 43 require.Equal(t, myjobs[0].Pid, pid1) 44 45 require.Equal(t, myjobs[1].Pid, pid2) 46 47 require.Equal(t, myjobs[2].Pid, pid3) 48 //test sort 49 myjobs.Sort() 50 51 require.Equal(t, myjobs[0].Pid, pid2) 52 require.Equal(t, myjobs[1].Pid, pid1) 53 require.Equal(t, myjobs[2].Pid, pid3) 54 55 //test delete 56 myjobs = myjobs.Remove(&taskInfo{Index: 4}) 57 require.Equal(t, 3, myjobs.Len()) 58 myjobs = myjobs.Remove(&taskInfo{Index: 0}) 59 require.Equal(t, 2, myjobs.Len()) 60 61 } 62 63 func TestJob(t *testing.T) { 64 testJobs(t) 65 }