good morning!!!!

Skip to content
Snippets Groups Projects
Select Git revision
  • 5b701962dc3b45f4cdcad01492adab02b0cac4a7
  • master default protected
  • v0.2.16-candidate
  • shivam/rpcAddBorTx
  • default-cli-config
  • shivam/minerRecommitFix
  • vcastellm/pos-296-bump-go-version-in-bor-and-heimdall
  • shivam/ethstats-backend-fix
  • v0.2.16-beta1-candidate
  • v0.2.15-beta3-candidate
  • shivam/newCli-IPC
  • v0.3.0-dev
  • checkpoint-whitelist-master
  • shivam/codecov
  • jdkanani/fix-typo-log
  • shivam/hardcoded-spans-v0.2.14
  • shivam/hardcoded-spans
  • shivam/fast-state-sync
  • shivam/fast-state-sync-master
  • gethv1.10.15-merge
  • fix-txpool-2
  • v0.2.14-tmp-span-hotfix
  • v0.2.15-beta2
  • v0.2.15-beta1
  • v0.3.0-beta3
  • v0.3.0-beta2
  • v0.3.0-beta1
  • v0.2.14
  • v0.2.13
  • v0.2.13-beta2
  • v0.2.13-beta1
  • v0.2.12
  • v0.2.12-beta3
  • v0.2.12-beta1
  • v0.2.12-beta2
  • v0.2.11
  • v0.2.10
  • v0.2.10-beta2
  • v0.2.9
  • v0.2.9-beta1
  • v0.2.8
41 results

index.js

Blame
  • Forked from github / maticnetwork / bor
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    scheduler_test.go 2.51 KiB
    package schedulers
    
    import (
    	"log"
    	"sync"
    	"testing"
    	"time"
    )
    
    type Work struct {
    	Sender   int
    	Duration time.Duration
    	Done     chan<- struct{}
    }
    
    type ShareTable struct {
    	table map[int]int
    	mu    sync.RWMutex
    }
    
    func (T *ShareTable) Inc(user int) {
    	T.mu.Lock()
    	defer T.mu.Unlock()
    
    	if T.table == nil {
    		T.table = make(map[int]int)
    	}
    	T.table[user]++
    }
    
    func (T *ShareTable) Get(user int) int {
    	T.mu.RLock()
    	defer T.mu.RUnlock()
    
    	v, _ := T.table[user]
    	return v
    }
    
    func testSink(sched *Scheduler, table *ShareTable) {
    	sink := sched.NewSink()
    	for {
    		w := sink.Read()
    		switch v := w.(type) {
    		case Work:
    			// dummy load
    			start := time.Now()
    			for time.Since(start) < v.Duration {
    			}
    			table.Inc(v.Sender)
    			close(v.Done)
    		}
    	}
    }
    
    func testSource(sched *Scheduler, id int, dur time.Duration) {
    	source := sched.NewSource()
    	for {
    		done := make(chan struct{})
    		w := Work{
    			Sender:   id,
    			Duration: dur,
    			Done:     done,
    		}
    		source.Schedule(w)
    		<-done
    	}
    }
    
    func TestScheduler(t *testing.T) {
    	var table ShareTable
    	sched := NewScheduler()
    	go testSink(sched, &table)
    
    	go testSource(sched, 0, 10*time.Millisecond)
    	go testSource(sched, 1, 10*time.Millisecond)
    	go testSource(sched, 2, 50*time.Millisecond)
    	go testSource(sched, 3, 100*time.Millisecond)
    
    	time.Sleep(20 * time.Second)
    	t0 := table.Get(0)
    	t1 := table.Get(1)
    	t2 := table.Get(2)
    	t3 := table.Get(3)
    	log.Println("share of 0:", t0)
    	log.Println("share of 1:", t1)
    	log.Println("share of 2:", t2)
    	log.Println("share of 3:", t3)
    
    	/*
    		Expectations:
    		- 0 and 1 should be similar and have roughly 10x of 3
    		- 2 should have about twice as many executions as 3
    	*/
    }
    
    func TestScheduler_Late(t *testing.T) {
    	var table ShareTable
    	sched := NewScheduler()
    	go testSink(sched, &table)
    
    	go testSource(sched, 0, 10*time.Millisecond)
    	go testSource(sched, 1, 10*time.Millisecond)
    
    	time.Sleep(10 * time.Second)
    
    	go testSource(sched, 2, 10*time.Millisecond)
    	go testSource(sched, 3, 10*time.Millisecond)
    
    	time.Sleep(10 * time.Second)
    	t0 := table.Get(0)
    	t1 := table.Get(1)
    	t2 := table.Get(2)
    	t3 := table.Get(3)
    	log.Println("share of 0:", t0)
    	log.Println("share of 1:", t1)
    	log.Println("share of 2:", t2)
    	log.Println("share of 3:", t3)
    
    	/*
    		Expectations:
    		- 0 and 1 should be similar
    		- 2 and 3 should be similar
    		- 0 and 1 should have roughly three times as many executions as 2 and 3
    
    		IF THEY ARE ROUGHLY SIMILAR, THIS TEST IS A FAIL!!!! 0 AND 1 SHOULD NOT STALL WHEN 2 AND 3 ARE INTRODUCED
    		i need to make these automatic, but it's easy enough to eyeball it
    	*/
    }