-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashgrab_test.go
77 lines (65 loc) · 2.38 KB
/
hashgrab_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
package hashgrab_test
import (
"fmt"
"crypto/sha256"
"github.com/tauki/hashgrab"
)
type customFetcherType struct{}
func (f *customFetcherType) Fetch(url string) ([]byte, error) {
// Implement custom fetching logic here
// This is a dummy implementation that returns URL as bytes and nil error
return []byte(url), nil
}
type customHasherType struct{}
func (h *customHasherType) Hash(data []byte) string {
// Implement custom hashing logic here
// This is a dummy implementation that returns SHA256 hash as hex
hash := sha256.Sum256(data)
return fmt.Sprintf("%x", hash)
}
// ExampleNew demonstrates how to create a new Worker.
func ExampleNew() {
_ = hashgrab.New()
fmt.Println("Created a new worker")
// Output: Created a new worker
}
// ExampleWorker_MaxWorker demonstrates how to set the maximum number of workers.
func ExampleWorker_MaxWorker() {
worker := hashgrab.New().MaxWorker(5)
fmt.Printf("Updated worker to have %v parallelism\n", worker.GetMaxWorker())
// Output: Updated worker to have 5 parallelism
}
// ExampleWorker_Fetcher demonstrates how to set a custom fetcher.
func ExampleWorker_Fetcher() {
worker := hashgrab.New()
customFetcher := hashgrab.NewFetcher() // Assumes you have a custom Fetcher
worker.Fetcher(customFetcher)
fmt.Println("Updated worker to use a custom fetcher")
// Output: Updated worker to use a custom fetcher
}
// ExampleWorker_Hasher demonstrates how to set a custom hasher.
func ExampleWorker_Hasher() {
worker := hashgrab.New()
customHasher := hashgrab.NewMD5Hasher() // Assumes you have a custom Hasher
worker.Hasher(customHasher)
fmt.Println("Updated worker to use a custom hasher")
// Output: Updated worker to use a custom hasher
}
// ExampleWorker_Run demonstrates how to start a Worker.
func ExampleWorker_Run() {
worker := hashgrab.New().MaxWorker(2)
worker.Fetcher(&customFetcherType{})
worker.Hasher(&customHasherType{})
urls := []string{"http://example.com", "http://example.org"}
responseChan := worker.Run(urls)
for response := range responseChan {
if response.Error != nil {
fmt.Printf("Error fetching %s: %v\n", response.Url, response.Error)
continue
}
fmt.Printf("Hash of %s: %s\n", response.Url, response.Hash)
}
// Output:
// Hash of http://example.org: 971a565c8ac770ff0b288d98a507bb832b8002214411ed8244d0b981a506dd3e
// Hash of http://example.com: f0e6a6a97042a4f1f1c87f5f7d44315b2d852c2df5c7991cc66241bf7072d1c4
}