From 74cdd935b986667b573d0f1cb745ba89b4483589 Mon Sep 17 00:00:00 2001 From: Ashwin Arvind Kulkarni Date: Mon, 9 Oct 2023 01:42:46 +0530 Subject: [PATCH] Added configurable parameter for time in the listener --- examples/base/main.go | 4 +++- pgsql_transport.go | 4 ++-- pgsql_transport_test.go | 2 +- transport.go | 6 +++--- transport_test.go | 4 ++-- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/examples/base/main.go b/examples/base/main.go index b3058a0..cf55179 100644 --- a/examples/base/main.go +++ b/examples/base/main.go @@ -22,7 +22,9 @@ func main() { TableName: "messenger_messages", } - err := gosumer.Listen(database, processMessage, Message{}) + sec := 5 + + err := gosumer.Listen(database, processMessage, Message{}, sec) if err != nil { log.Fatal(err) diff --git a/pgsql_transport.go b/pgsql_transport.go index 564ba6b..bc0b903 100644 --- a/pgsql_transport.go +++ b/pgsql_transport.go @@ -31,7 +31,7 @@ func (database PgDatabase) connect() error { return nil } -func (database PgDatabase) listen(fn process, message any) error { +func (database PgDatabase) listen(fn process, message any, sec int) error { err := database.connect() if err != nil { @@ -40,7 +40,7 @@ func (database PgDatabase) listen(fn process, message any) error { defer pool.Close() - database.listenEvery(5, fn, message) + database.listenEvery(sec, fn, message) log.Printf("Successfully connected to the database!") diff --git a/pgsql_transport_test.go b/pgsql_transport_test.go index 10977d6..337f40e 100644 --- a/pgsql_transport_test.go +++ b/pgsql_transport_test.go @@ -85,7 +85,7 @@ func TestPgListen(t *testing.T) { defer pool.Close() go func() { - err := database.listen(processMessage, Message{}) + err := database.listen(processMessage, Message{}, 5) if err != nil { t.Errorf("Expected no error, got %v", err) } diff --git a/transport.go b/transport.go index 68440fb..eff57af 100644 --- a/transport.go +++ b/transport.go @@ -6,7 +6,7 @@ import ( type Transport interface { connect() error - listen(fn process, message any) error + listen(fn process, message any, sec int) error } func formatMessage(message string, msg any) (any, error) { @@ -17,8 +17,8 @@ func formatMessage(message string, msg any) (any, error) { return msg, nil } -func Listen(transport Transport, fn process, message any) error { - err := transport.listen(fn, message) +func Listen(transport Transport, fn process, message any, sec int) error { + err := transport.listen(fn, message, sec) if err != nil { return err } diff --git a/transport_test.go b/transport_test.go index 3c3d902..bd31ad9 100644 --- a/transport_test.go +++ b/transport_test.go @@ -31,7 +31,7 @@ func (transport TestTransport) connect() error { return nil } -func (transport TestTransport) listen(fn process, message any) error { +func (transport TestTransport) listen(fn process, message any, sec int) error { go fn(message, make(chan error)) return nil @@ -51,7 +51,7 @@ type Message struct { func TestListen(t *testing.T) { transport := TestTransport{} - err := Listen(transport, processMessage, Message{}) + err := Listen(transport, processMessage, Message{}, 5) if err != nil { t.Errorf("Expected no error, got %v", err) }