aboutsummaryrefslogtreecommitdiff
path: root/auction_test.go
blob: 5f2b3e12c07320d8b0e785b059eac0dcf26fa362 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package seal

import (
	"encoding/json"
	"fmt"
	"log/slog"
	"math/rand"
	"strings"
	"sync"
	"testing"
	"time"
)

func TestParseDescription(t *testing.T) {
	var d Description
	var desc = `
	{
		"start": "2024-11-10T15:04:05Z",
		"end": "2024-11-10T18:04:05Z",
		"timeout": "10s",
		"bitlength": 8,
		"currency": "KUDOS",
		"type": 0,
		"seller": "1234"
	}
`
	e := json.NewDecoder(strings.NewReader(desc)).
		Decode(&d)
	if e != nil {
		t.Fatal(e)
	}

	if time.Duration(d.Timeout) != 10*time.Second {
		t.Fatalf("expected timeout 10s, but got: %v", time.Duration(d.Timeout))
	}

	if d.Bitlength != 8 {
		t.Fatalf("expected bitlength %d, but got: %d", 8, d.Bitlength)
	}
}

type bidder struct {
	bid uint64
}

func (b *bidder) Broadcast(m Message) error {
	return fmt.Errorf("Broadcast not implemented")
}

func (b *bidder) Result(r Result) {
	fmt.Printf("got result: %v\n", r)
}

func (b *bidder) Start() error {
	return fmt.Errorf("bidder start not fully implemented")
}

func TestAuction(t *testing.T) {
	var bidders [10]struct {
		b bidder
		a Auction
	}
	d := Description{
		Start:     time.Now(),
		End:       time.Now().Add(time.Hour),
		Timeout:   Duration(time.Minute),
		Bitlength: 8,
		Type:      TypHighest,
		Seller:    nil,
	}

	for i := range bidders {
		bidders[i].b.bid = rand.Uint64() & 0xFF
		a, e := Join(&bidders[i].b, &d)
		if e != nil {
			t.Fatal(e)
		}
		if e != nil {
			t.Fatal(e)
		}
		bidders[i].a = a
	}

	var wg sync.WaitGroup

	wg.Add(len(bidders))
	for i, b := range bidders {
		go func() {
			e := b.a.Start()
			if e != nil {
				(b.a.(*auction)).log.Error(e.Error(), slog.Int("id", i), slog.Uint64("bid", b.b.bid))
			}
			wg.Done()
		}()
	}
	wg.Wait()
}