Skip to content

Commit f21a654

Browse files
authored
Merge pull request #11 from simodima/improves-test-startup
Implements consumer Ping()
2 parents a61bad0 + 26693b0 commit f21a654

File tree

12 files changed

+137
-57
lines changed

12 files changed

+137
-57
lines changed

README.md

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,58 @@
1-
<picture>
2-
<source media="(prefers-color-scheme: dark)" srcset="./docs/squeue-white.png">
3-
<source media="(prefers-color-scheme: light)" srcset="./docs/squeue-black.png">
4-
<img alt="S-queue mascotte" src="./docs/squeue-black.png">
1+
<picture>
2+
<source media="(prefers-color-scheme: dark)" srcset="./docs/squeue-white.png">
3+
<source media="(prefers-color-scheme: light)" srcset="./docs/squeue-black.png">
4+
<img alt="S-queue mascot" src="./docs/squeue-black.png">
55
</picture>
66

7-
**s-queue** is a library to interact with Amazon SQS queues with 100% type safety, powered by golang generics.
8-
7+
**squeue** is a Golang library designed to facilitate interactions with Amazon SQS queues, providing 100% type-safety powered by Golang generics.
8+
99
---
10-
11-
[![codecov](https://codecov.io/github/simodima/squeue/graph/badge.svg?token=DW7C57P2VW)](https://codecov.io/github/simodima/squeue)
12-
[![Go Report Card](https://goreportcard.com/badge/github.com/simodima/squeue)](https://goreportcard.com/report/github.com/simodima/squeue)
10+
11+
[![codecov](https://codecov.io/github/simodima/squeue/graph/badge.svg?token=DW7C57P2VW)](https://codecov.io/github/simodima/squeue)
12+
[![Go Report Card](https://goreportcard.com/badge/github.com/simodima/squeue)](https://goreportcard.com/report/github.com/simodima/squeue)
1313
[![Go Reference](https://pkg.go.dev/badge/github.com/simodima/squeue.svg)](https://pkg.go.dev/github.com/simodima/squeue)
1414

15-
The library provides the following features:
15+
## Key Features
1616

17-
### Driver based implementation
18-
The interface exposed by the library abstract the internal driver implementation, that allows to plug various different drivers depending of the development needs and the environment.
17+
### Driver-based Implementation
18+
squeue abstracts the internal driver implementation through its interface, allowing for seamless integration of various drivers to suit different development environments and needs.
1919

20-
The currently implemented drivers are :
21-
- In Memory
20+
Currently implemented drivers:
21+
- In-Memory
2222
- Amazon SQS
2323

24-
You can easly use the *Amazon SQS* driver in any staging/production environment and use the *In Memory* one for a lightweight testing environment.
24+
You can easily deploy the *Amazon SQS* driver in any staging or production environment and use the *In-Memory* driver for lightweight testing.
2525

2626
### Type Safety
27-
The library uses an opinionated JSON format to serialize/deserialize the messages. To be 100% typesafe you just need to provide a message type implementing the golang `json.Marshaler` and `json.Unmarshaler` interfaces; in that way the raw data serialization/deserialization is in your direct control.
27+
The library uses an opinionated JSON format for serializing and deserializing messages. To achieve 100% type safety, simply provide a message type that implements the Golang `json.Marshaler` and `json.Unmarshaler` interfaces. This way, you maintain direct control over the raw data serialization and deserialization.
2828

29-
### Channel of messages
30-
Consuming message is as easy as looping a channel of messages
29+
### Channel of Messages
30+
Consuming messages is straightforward—just loop through a channel of messages:
3131

3232
```golang
33-
messages, _ := sub.Consume(ctx, "queue")
34-
for m := range messages {
35-
go func(message squeue.Message[*my.Message]) {
36-
// Do the magic...
37-
}(m)
38-
}
33+
messages, _ := sub.Consume(ctx, "queue")
34+
for m := range messages {
35+
go func(message squeue.Message[*my.Message]) {
36+
// Do your magic...
37+
}(m)
38+
}
3939
```
4040

4141
<details>
42+
<summary>Examples</summary>
4243

43-
<summary>Examples</summary>
44-
45-
For a more clear documentation look at the `internal/examples/` directory
44+
For more detailed documentation, please refer to the `internal/examples/` directory.
4645

47-
**In Memory driver**
48-
```bash
49-
go run internal/examples/memory/main.go
50-
```
51-
52-
**Amazon SQS driver**
53-
```bash
54-
go run internal/examples/sqs/consumer/consumer.go
46+
**In-Memory Driver**
47+
```bash
48+
go run internal/examples/memory/main.go
49+
```
5550

56-
## in a different shell ↓
57-
go run internal/examples/sqs/producer/producer.go
58-
```
51+
**Amazon SQS Driver**
52+
```bash
53+
go run internal/examples/sqs/consumer/consumer.go
54+
# Open another shell
55+
go run internal/examples/sqs/producer/producer.go
56+
```
5957

60-
</details>
58+
</details>

consumer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ func (p *Consumer[T]) Consume(opts ...func(message any)) (chan Message[T], error
6565
return outMsg, nil
6666
}
6767

68+
// Ping runs a ping on the underlying driver
69+
func (p Consumer[T]) Ping() error {
70+
return p.driver.Ping()
71+
}
72+
6873
func (p *Consumer[T]) Stop() {
6974
if p.controller != nil {
7075
p.controller.Stop()

consumer_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ func (suite *ConsumerTestSuite) TestNewConsumer() {
3333
squeue.NewConsumer[*TestMessage](suite.driver, "test-queue")
3434
}
3535

36+
func (suite *ConsumerTestSuite) TestPing() {
37+
queue := "test-queue"
38+
consumer := squeue.NewConsumer[*TestMessage](suite.driver, queue)
39+
40+
suite.driver.
41+
EXPECT().
42+
Ping().
43+
Return(errors.New("consume error"))
44+
45+
err := consumer.Ping()
46+
suite.Error(err)
47+
}
48+
3649
func (suite *ConsumerTestSuite) TestConsumeMessages_DriverError() {
3750
queue := "test-queue"
3851
consumer := squeue.NewConsumer[*TestMessage](suite.driver, queue)

driver/driver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ type Driver interface {
3333
Enqueue(queue string, data []byte, opts ...func(message any)) error
3434
Consume(queue string, opts ...func(message any)) (*ConsumerController, error)
3535
Ack(queue string, messageID string) error
36+
Ping() error
3637
}

driver/memdriver.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ func (d *MemoryDriver) pop(queue string) *Message {
7676

7777
return nil
7878
}
79+
80+
func (d *MemoryDriver) Ping() error {
81+
return nil
82+
}

driver/memdriver_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ func (suite *MemoryTestSuite) TestAckDONotBreakAnything() {
2929
suite.Nil(d.Ack("test", "1"))
3030
}
3131

32+
func (suite *MemoryTestSuite) TestPingDONotBreakAnything() {
33+
d := driver.NewMemoryDriver(time.Millisecond)
34+
suite.Nil(d.Ping())
35+
}
36+
3237
func (suite *MemoryTestSuite) TestEnqueueDequeueSuccess() {
3338
d := driver.NewMemoryDriver(time.Millisecond)
3439

driver_test.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/examples/sqs/consumer/consumer.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os/signal"
77
"sync"
88
"syscall"
9+
"time"
910

1011
"github.com/joho/godotenv"
1112
"github.com/simodima/squeue"
@@ -61,12 +62,24 @@ func main() {
6162

6263
messages, err := sub.Consume(
6364
sqs.WithConsumeMaxNumberOfMessages(10),
64-
sqs.WithConsumeWaitTimeSeconds(20),
65+
sqs.WithConsumeWaitTimeSeconds(2),
6566
)
6667
if err != nil {
6768
panic(err)
6869
}
6970

71+
// Ping the sqs to check connectivity with AWS
72+
go func() {
73+
for {
74+
<-time.After(time.Second * 10)
75+
if err := sub.Ping(); err != nil {
76+
log.Println("Ping failed: " + err.Error())
77+
} else {
78+
log.Println("Ping OK")
79+
}
80+
}
81+
}()
82+
7083
log.Print("Waiting for consuming")
7184
wg := &sync.WaitGroup{}
7285

sqs/mocks/sqsclient.go

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqs/sqs.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type sqsClient interface {
1717
DeleteMessage(input *sqs.DeleteMessageInput) (*sqs.DeleteMessageOutput, error)
1818
SendMessage(input *sqs.SendMessageInput) (*sqs.SendMessageOutput, error)
1919
ReceiveMessage(input *sqs.ReceiveMessageInput) (*sqs.ReceiveMessageOutput, error)
20-
ListQueues(input *sqs.ListQueuesInput) (*sqs.ListQueuesOutput, error)
20+
GetQueueAttributes(input *sqs.GetQueueAttributesInput) (*sqs.GetQueueAttributesOutput, error)
2121
}
2222

2323
type Driver struct {
@@ -51,7 +51,7 @@ func New(options ...Option) (*Driver, error) {
5151
}
5252

5353
if driver.testConnectionOnStartup {
54-
if err := driver.testConnection(); err != nil {
54+
if err := driver.Ping(); err != nil {
5555
return nil, err
5656
}
5757
}
@@ -87,8 +87,3 @@ func createClient(queueUrl string, region string, clientCredentials *credentials
8787

8888
return sqs.New(session.Must(session.NewSessionWithOptions(options))), nil
8989
}
90-
91-
func (d *Driver) testConnection() error {
92-
_, err := d.sqsClient.ListQueues(&sqs.ListQueuesInput{})
93-
return err
94-
}

0 commit comments

Comments
 (0)