• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

AlexsanderHamir / PoolX / 15052547260

15 May 2025 06:31PM UTC coverage: 16.359% (-0.08%) from 16.436%
15052547260

push

github

AlexsanderHamir
Merge branch 'main' of github.com:AlexsanderHamir/PoolX

355 of 2170 relevant lines covered (16.36%)

4445.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.27
/code_examples/basic_example.go
1
package code_examples
2

3
import (
4
        "context"
5
        "fmt"
6
        "math/rand"
7
        "sync"
8
        "time"
9

10
        "github.com/AlexsanderHamir/PoolX/v2/code_examples/configs"
11
        "github.com/AlexsanderHamir/PoolX/v2/pool"
12
)
13

14
func RunBasicExample() error {
1✔
15
        config := configs.CreateHighThroughputConfig()
1✔
16
        pool, err := pool.NewPool(
1✔
17
                config,
1✔
18
                func() *configs.Example { return &configs.Example{Name: "test"} },
3✔
19
                func(obj *configs.Example) {
200,512✔
20
                        obj.Name = ""
200,512✔
21
                },
200,512✔
22
                func(obj *configs.Example) *configs.Example {
20,001✔
23
                        dst := *obj
20,001✔
24
                        return &dst
20,001✔
25
                },
20,001✔
26
        )
27
        if err != nil {
1✔
28
                return err
×
29
        }
×
30
        defer pool.Close()
1✔
31

1✔
32
        const numWorkers = 2000
1✔
33
        const objectsPerWorker = 100
1✔
34

1✔
35
        ctx, cancel := context.WithCancel(context.Background())
1✔
36
        defer cancel()
1✔
37

1✔
38
        errChan := make(chan error, numWorkers)
1✔
39
        var wg sync.WaitGroup
1✔
40

1✔
41
        for i := range numWorkers {
2,001✔
42
                wg.Add(1)
2,000✔
43
                go func(workerID int) {
4,000✔
44
                        defer wg.Done()
2,000✔
45

2,000✔
46
                        for j := range objectsPerWorker {
202,000✔
47
                                select {
200,000✔
48
                                case <-ctx.Done():
×
49
                                        return
×
50
                                default:
200,000✔
51
                                        obj, err := pool.Get()
200,000✔
52
                                        if err != nil {
200,000✔
53
                                                errChan <- fmt.Errorf("worker %d failed to get object: %w", workerID, err)
×
54
                                                cancel()
×
55
                                                return
×
56
                                        }
×
57
                                        obj.Name = fmt.Sprintf("Worker-%d-Object-%d", workerID, j)
200,000✔
58

200,000✔
59
                                        time.Sleep(time.Duration(rand.Float64() * float64(time.Millisecond*400)))
200,000✔
60
                                        if err := pool.Put(obj); err != nil {
200,000✔
61
                                                errChan <- fmt.Errorf("worker %d failed to put object: %w", workerID, err)
×
62
                                                cancel()
×
63
                                                return
×
64
                                        }
×
65
                                }
66
                        }
67
                }(i)
68
        }
69

70
        go func() {
2✔
71
                wg.Wait()
1✔
72
                close(errChan)
1✔
73
        }()
1✔
74

75
        if err := <-errChan; err != nil {
1✔
76
                return err
×
77
        }
×
78

79
        pool.PrintPoolStats()
1✔
80
        return nil
1✔
81
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc