-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First implementation for Exchanges and bindings (#2)
* First implementation for Exchanges and bindings --------- Signed-off-by: Gabriele Santomaggio <[email protected]>
- Loading branch information
1 parent
fa7d5d9
commit 05f7cd9
Showing
13 changed files
with
435 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package rabbitmq_amqp | ||
|
||
import "context" | ||
|
||
type AMQPBindingInfo struct { | ||
} | ||
|
||
type AMQPBinding struct { | ||
sourceExchangeName string | ||
destinationQueue string | ||
bindingKey string | ||
management *AmqpManagement | ||
} | ||
|
||
func newAMQPBinding(management *AmqpManagement) *AMQPBinding { | ||
return &AMQPBinding{management: management} | ||
} | ||
|
||
func (b *AMQPBinding) Key(bindingKey string) IBindingSpecification { | ||
b.bindingKey = bindingKey | ||
return b | ||
} | ||
|
||
func (b *AMQPBinding) SourceExchange(exchangeName string) IBindingSpecification { | ||
b.sourceExchangeName = exchangeName | ||
return b | ||
} | ||
|
||
func (b *AMQPBinding) DestinationQueue(queueName string) IBindingSpecification { | ||
b.destinationQueue = queueName | ||
return b | ||
} | ||
|
||
func (b *AMQPBinding) Bind(ctx context.Context) error { | ||
|
||
path := bindingPath() | ||
kv := make(map[string]any) | ||
kv["binding_key"] = b.bindingKey | ||
kv["source"] = b.sourceExchangeName | ||
kv["destination_queue"] = b.destinationQueue | ||
kv["arguments"] = make(map[string]any) | ||
_, err := b.management.Request(ctx, kv, path, commandPost, []int{responseCode204}) | ||
return err | ||
|
||
} | ||
|
||
func (b *AMQPBinding) Unbind(ctx context.Context) error { | ||
bindingPathWithExchangeQueueKey := bindingPathWithExchangeQueueKey(b.sourceExchangeName, b.destinationQueue, b.bindingKey) | ||
_, err := b.management.Request(ctx, nil, bindingPathWithExchangeQueueKey, commandDelete, []int{responseCode204}) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package rabbitmq_amqp | ||
|
||
import ( | ||
"context" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("AMQP Bindings test ", func() { | ||
|
||
var connection IConnection | ||
var management IManagement | ||
BeforeEach(func() { | ||
connection = NewAmqpConnection() | ||
Expect(connection).NotTo(BeNil()) | ||
Expect(connection).To(BeAssignableToTypeOf(&AmqpConnection{})) | ||
connectionSettings := NewConnectionSettings() | ||
Expect(connectionSettings).NotTo(BeNil()) | ||
Expect(connectionSettings).To(BeAssignableToTypeOf(&ConnectionSettings{})) | ||
err := connection.Open(context.TODO(), connectionSettings) | ||
Expect(err).To(BeNil()) | ||
management = connection.Management() | ||
}) | ||
|
||
AfterEach(func() { | ||
Expect(connection.Close(context.Background())).To(BeNil()) | ||
}) | ||
|
||
It("AMQP Bindings between Exchange and Queue Should success ", func() { | ||
const exchangeName = "Exchange_AMQP Bindings between Exchange and Queue Should success" | ||
const queueName = "Queue_AMQP Bindings between Exchange and Queue Should success" | ||
exchangeSpec := management.Exchange(exchangeName) | ||
exchangeInfo, err := exchangeSpec.Declare(context.TODO()) | ||
Expect(err).To(BeNil()) | ||
Expect(exchangeInfo).NotTo(BeNil()) | ||
Expect(exchangeInfo.GetName()).To(Equal(exchangeName)) | ||
|
||
queueSpec := management.Queue(queueName) | ||
queueInfo, err := queueSpec.Declare(context.TODO()) | ||
Expect(err).To(BeNil()) | ||
Expect(queueInfo).NotTo(BeNil()) | ||
Expect(queueInfo.GetName()).To(Equal(queueName)) | ||
|
||
bindingSpec := management.Binding().SourceExchange(exchangeName). | ||
DestinationQueue(queueName). | ||
Key("routing-key") | ||
err = bindingSpec.Bind(context.TODO()) | ||
Expect(err).To(BeNil()) | ||
err = bindingSpec.Unbind(context.TODO()) | ||
Expect(err).To(BeNil()) | ||
err = exchangeSpec.Delete(context.TODO()) | ||
Expect(err).To(BeNil()) | ||
err = queueSpec.Delete(context.TODO()) | ||
Expect(err).To(BeNil()) | ||
|
||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package rabbitmq_amqp | ||
|
||
import "context" | ||
|
||
type AmqpExchangeInfo struct { | ||
name string | ||
} | ||
|
||
func newAmqpExchangeInfo(name string) IExchangeInfo { | ||
return &AmqpExchangeInfo{name: name} | ||
} | ||
|
||
func (a *AmqpExchangeInfo) GetName() string { | ||
return a.name | ||
} | ||
|
||
type AmqpExchange struct { | ||
name string | ||
management *AmqpManagement | ||
arguments map[string]any | ||
isAutoDelete bool | ||
exchangeType ExchangeType | ||
} | ||
|
||
func newAmqpExchange(management *AmqpManagement, name string) *AmqpExchange { | ||
return &AmqpExchange{management: management, | ||
name: name, | ||
arguments: make(map[string]any), | ||
exchangeType: ExchangeType{Type: Direct}, | ||
} | ||
} | ||
|
||
func (e *AmqpExchange) Declare(ctx context.Context) (IExchangeInfo, error) { | ||
|
||
path := exchangePath(e.name) | ||
kv := make(map[string]any) | ||
kv["auto_delete"] = e.isAutoDelete | ||
kv["durable"] = true | ||
kv["type"] = e.exchangeType.String() | ||
kv["arguments"] = e.arguments | ||
_, err := e.management.Request(ctx, kv, path, commandPut, []int{responseCode204, responseCode201, responseCode409}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return newAmqpExchangeInfo(e.name), nil | ||
} | ||
|
||
func (e *AmqpExchange) AutoDelete(isAutoDelete bool) IExchangeSpecification { | ||
e.isAutoDelete = isAutoDelete | ||
return e | ||
} | ||
|
||
func (e *AmqpExchange) IsAutoDelete() bool { | ||
return e.isAutoDelete | ||
} | ||
|
||
func (e *AmqpExchange) Delete(ctx context.Context) error { | ||
path := exchangePath(e.name) | ||
_, err := e.management.Request(ctx, nil, path, commandDelete, []int{responseCode204}) | ||
return err | ||
} | ||
|
||
func (e *AmqpExchange) ExchangeType(exchangeType ExchangeType) IExchangeSpecification { | ||
e.exchangeType = exchangeType | ||
return e | ||
} | ||
|
||
func (e *AmqpExchange) GetExchangeType() TExchangeType { | ||
return e.exchangeType.Type | ||
} | ||
|
||
func (e *AmqpExchange) GetName() string { | ||
return e.name | ||
} |
Oops, something went wrong.