Skip to content

Commit 639f361

Browse files
committed
Make client an interface for mocks
1 parent 108469d commit 639f361

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import (
2424
"github.com/multiformats/go-multiaddr"
2525
)
2626

27+
// Compile-time check to ensure Client implements P2PClient interface
28+
var _ P2PClient = (*Client)(nil)
29+
2730
// Client represents a P2P messaging client.
2831
type Client struct {
2932
config Config
@@ -43,7 +46,7 @@ type Client struct {
4346

4447
// NewClient creates and initializes a new P2P client.
4548
// It automatically starts the client and begins peer discovery.
46-
func NewClient(config Config) (*Client, error) {
49+
func NewClient(config Config) (P2PClient, error) {
4750
if config.Name == "" {
4851
return nil, fmt.Errorf("config.Name is required")
4952
}

types.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
package p2p
22

33
import (
4+
"context"
45
"sync"
56
"time"
67

78
"github.com/libp2p/go-libp2p/core/peer"
89
)
910

11+
// P2PClient defines the interface for a P2P messaging client.
12+
type P2PClient interface {
13+
// Subscribe subscribes to a topic and returns a channel that will receive messages.
14+
// The returned channel will be closed when the client is closed.
15+
Subscribe(topic string) <-chan Message
16+
17+
// Publish publishes a message to the specified topic.
18+
Publish(ctx context.Context, topic string, data []byte) error
19+
20+
// GetPeers returns information about all known peers on subscribed topics.
21+
GetPeers() []PeerInfo
22+
23+
// GetID returns this peer's ID as a string.
24+
GetID() string
25+
26+
// Close shuts down the client and releases all resources.
27+
Close() error
28+
}
29+
1030
// Message represents a received message from a peer.
1131
type Message struct {
1232
Topic string // The topic this message was received on

0 commit comments

Comments
 (0)