decred.org/dcrdex@v1.0.5/server/auth/latest_test.go (about)

     1  package auth
     2  
     3  import (
     4  	"bytes"
     5  	"math/rand"
     6  	"sort"
     7  	"testing"
     8  
     9  	"decred.org/dcrdex/dex/order"
    10  )
    11  
    12  func Test_latestMatchOutcomes(t *testing.T) {
    13  	cap := int16(10)
    14  	ordList := newLatestMatchOutcomes(cap)
    15  
    16  	randTime := func() int64 {
    17  		return 1600477631 + rand.Int63n(987654)
    18  	}
    19  
    20  	N := int(cap + 20)
    21  	times := make([]int64, N)
    22  	for i := 0; i < N; i++ {
    23  		t := randTime()
    24  		times[i] = t
    25  		ordList.add(&matchOutcome{
    26  			time: t,
    27  			mid:  randomMatchID(),
    28  		})
    29  	}
    30  
    31  	sort.Slice(times, func(i, j int) bool {
    32  		return times[i] < times[j] // ascending
    33  	})
    34  
    35  	if len(ordList.outcomes) != int(cap) {
    36  		t.Fatalf("latest list is length %d, wanted %d", len(ordList.outcomes), cap)
    37  	}
    38  
    39  	// ensure the ordList contains the N most recent items, with the oldest at
    40  	// the front of the list.
    41  	wantStampedTimes := times[len(times)-int(cap):] // grab the last cap items in the sorted ground truth list
    42  	for i, st := range ordList.outcomes {
    43  		if st.time != wantStampedTimes[i] {
    44  			t.Fatalf("Time #%d is %d, wanted %d", i, st.time, wantStampedTimes[i])
    45  		}
    46  	}
    47  
    48  	// Add 3 orders with the same time, different order IDs.
    49  	nextTime := 1 + wantStampedTimes[len(wantStampedTimes)-1] // new
    50  	mids := []order.MatchID{randomMatchID(), randomMatchID(), randomMatchID()}
    51  	for i := range mids {
    52  		ordList.add(&matchOutcome{
    53  			time: nextTime,
    54  			mid:  mids[i],
    55  		})
    56  	}
    57  
    58  	sort.Slice(mids, func(i, j int) bool {
    59  		return bytes.Compare(mids[i][:], mids[j][:]) < 0
    60  	})
    61  
    62  	if len(ordList.outcomes) != int(cap) {
    63  		t.Fatalf("latest list is length %d, wanted %d", len(ordList.outcomes), cap)
    64  	}
    65  
    66  	// Verify that the last three outcomes are the three we just added with the
    67  	// newest time stamp, and that they are storted according to oid.
    68  	for i, oc := range ordList.outcomes[cap-3 : cap] {
    69  		if oc.mid != mids[i] {
    70  			t.Errorf("Wrong mid #%d. got %v, want %v", i, oc.mid, mids[i])
    71  		}
    72  	}
    73  }
    74  
    75  func Test_latestPreimageOutcomes(t *testing.T) {
    76  	cap := int16(10)
    77  	ordList := newLatestPreimageOutcomes(cap)
    78  
    79  	randTime := func() int64 {
    80  		return 1600477631 + rand.Int63n(987654)
    81  	}
    82  
    83  	N := int(cap + 20)
    84  	times := make([]int64, N)
    85  	for i := 0; i < N; i++ {
    86  		t := randTime()
    87  		times[i] = t
    88  		ordList.add(&preimageOutcome{
    89  			time: t,
    90  			oid:  randomOrderID(),
    91  		})
    92  	}
    93  
    94  	sort.Slice(times, func(i, j int) bool {
    95  		return times[i] < times[j] // ascending
    96  	})
    97  
    98  	if len(ordList.outcomes) != int(cap) {
    99  		t.Fatalf("latest list is length %d, wanted %d", len(ordList.outcomes), cap)
   100  	}
   101  
   102  	// ensure the ordList contains the N most recent items, with the oldest at
   103  	// the front of the list.
   104  	wantStampedTimes := times[len(times)-int(cap):] // grab the last cap items in the sorted ground truth list
   105  	for i, st := range ordList.outcomes {
   106  		if st.time != wantStampedTimes[i] {
   107  			t.Fatalf("Time #%d is %d, wanted %d", i, st.time, wantStampedTimes[i])
   108  		}
   109  	}
   110  
   111  	// Add 3 orders with the same time, different order IDs.
   112  	nextTime := 1 + wantStampedTimes[len(wantStampedTimes)-1] // new
   113  	oids := []order.OrderID{randomOrderID(), randomOrderID(), randomOrderID()}
   114  	for i := range oids {
   115  		ordList.add(&preimageOutcome{
   116  			time: nextTime,
   117  			oid:  oids[i],
   118  		})
   119  	}
   120  
   121  	sort.Slice(oids, func(i, j int) bool {
   122  		return bytes.Compare(oids[i][:], oids[j][:]) < 0
   123  	})
   124  
   125  	if len(ordList.outcomes) != int(cap) {
   126  		t.Fatalf("latest list is length %d, wanted %d", len(ordList.outcomes), cap)
   127  	}
   128  
   129  	// Verify that the last three outcomes are the three we just added with the
   130  	// newest time stamp, and that they are storted according to oid.
   131  	for i, oc := range ordList.outcomes[cap-3 : cap] {
   132  		if oc.oid != oids[i] {
   133  			t.Errorf("Wrong oid #%d. got %v, want %v", i, oc.oid, oids[i])
   134  		}
   135  	}
   136  }