-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
145 lines (115 loc) · 4.26 KB
/
example_test.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package protoconf_loader
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"sync/atomic"
"time"
"github.com/protoconf/client-go/mockagent"
"github.com/protoconf/protoconf/examples/protoconf/src/crawler"
"google.golang.org/protobuf/proto"
)
func ExampleConfiguration() {
// Initialize the configuration with the default configuration.
crawlerConfig := &crawler.CrawlerService{LogLevel: 3}
// Create a new mock agent.
mock := mockagent.NewProtoconfAgentMock(crawlerConfig)
// Create a new configuration with the crawler config and the mock agent.
config, err := NewConfiguration(crawlerConfig, "crawler/config", WithAgentStub(mock.Stub))
if err != nil {
log.Fatalf("Error creating configuration: %v", err)
}
// Start the configuration watcher.
err = config.WatchConfig(context.Background())
if err != nil {
log.Fatal(err)
}
// Mock a configuration update.
mock.SendUpdate(&crawler.CrawlerService{LogLevel: 17})
// Wait for the configuration to be updated.
time.Sleep(1 * time.Millisecond)
fmt.Printf("Config changed: %v", crawlerConfig)
// Output: Config changed: log_level:17
}
func ExampleConfiguration_OnConfigChange() {
// Initialize the configuration with the default configuration.
crawlerConfig := &crawler.CrawlerService{LogLevel: 3}
// Create a new mock agent.
mock := mockagent.NewProtoconfAgentMock(crawlerConfig)
// Create a new configuration with the crawler config and the mock agent.
config, err := NewConfiguration(crawlerConfig, "crawler/config", WithAgentStub(mock.Stub))
if err != nil {
log.Fatalf("Error creating configuration: %v", err)
}
// Start the configuration watcher.
err = config.WatchConfig(context.Background())
if err != nil {
log.Fatal(err)
}
// Create a channel to receive configuration updates.
configUpdates := make(chan *crawler.CrawlerService)
config.OnConfigChange(func(c proto.Message) {
configUpdates <- c.(*crawler.CrawlerService)
})
// Mock a configuration update.
mock.SendUpdate(&crawler.CrawlerService{LogLevel: 17})
// Wait for the configuration to be updated.
time.Sleep(1 * time.Millisecond)
// Receive the configuration update.
newConfig := <-configUpdates
fmt.Printf("Config changed: %v", newConfig)
// Output: Config changed: log_level:17
}
func ExampleConfiguration_concurrencySafety() {
// Initialize the configuration with the default configuration.
config := func() *atomic.Pointer[crawler.CrawlerService] {
crawlerConfig := &crawler.CrawlerService{LogLevel: 3}
configHolder := &atomic.Pointer[crawler.CrawlerService]{}
configHolder.Store(crawlerConfig)
// Create a new mock agent.
mock := mockagent.NewProtoconfAgentMock(crawlerConfig)
// Create a new configuration with the crawler config and the mock agent.
config, err := NewConfiguration(crawlerConfig, "crawler/config", WithAgentStub(mock.Stub))
if err != nil {
log.Fatalf("Error creating configuration: %v", err)
}
// Start the configuration watcher.
err = config.WatchConfig(context.Background())
if err != nil {
log.Fatal(err)
}
// Update the configuration holder when the configuration changes.
config.OnConfigChange(func(c proto.Message) {
configHolder.Store(c.(*crawler.CrawlerService))
})
mock.SendUpdate(&crawler.CrawlerService{LogLevel: 18})
return configHolder
}()
// Mock a configuration update.
// Wait for the configuration to be updated.
time.Sleep(1 * time.Millisecond)
// Receive the configuration update.
fmt.Printf("Config changed: %v", config.Load())
// Output: Config changed: log_level:18
}
func ExampleConfiguration_watchConfigOnDisk() {
crawlerConfig := &crawler.CrawlerService{LogLevel: 3}
tempDir := os.TempDir()
configFilePath := filepath.Join(tempDir, "crawler_config.json")
os.WriteFile(configFilePath, []byte("{\"logLevel\": 3}"), 0644)
config, err := NewConfiguration(crawlerConfig, "crawler/config")
if err != nil {
log.Fatalf("Error creating configuration: %v", err)
}
config.LoadConfig(tempDir, "crawler_config.json")
fmt.Println("Config loaded from disk:", crawlerConfig)
config.WatchConfig(context.Background())
os.WriteFile(configFilePath, []byte("{\"logLevel\": 4}"), 0644)
time.Sleep(1 * time.Millisecond)
fmt.Println("Config updated on disk:", crawlerConfig)
// Output:
// Config loaded from disk: log_level:3
// Config updated on disk: log_level:4
}