-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recreate listener if error is occured (#236)
* Recreate listener if error is occured Signed-off-by: clyang82 <chuyang@redhat.com> * Fix integration test Signed-off-by: clyang82 <chuyang@redhat.com> * fix integration test Signed-off-by: clyang82 <chuyang@redhat.com> --------- Signed-off-by: clyang82 <chuyang@redhat.com>
- Loading branch information
Showing
4 changed files
with
92 additions
and
16 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
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,62 @@ | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
"github.com/openshift-online/maestro/test" | ||
) | ||
|
||
func TestWaitForNotification(t *testing.T) { | ||
// it is used to check the result of the notification | ||
result := make(chan string) | ||
|
||
h, _ := test.RegisterIntegration(t) | ||
|
||
account := h.NewRandAccount() | ||
ctx, cancel := context.WithCancel(h.NewAuthenticatedContext(account)) | ||
defer func() { | ||
cancel() | ||
}() | ||
|
||
g2 := h.Env().Database.SessionFactory.New(ctx) | ||
listener := h.Env().Database.SessionFactory.NewListener(ctx, "channel", func(id string) { | ||
result <- id | ||
}) | ||
var originalListenerId string | ||
// find the original listener id in the pg_stat_activity table | ||
g2.Raw("SELECT pid FROM pg_stat_activity WHERE query LIKE 'LISTEN%channel%'").Scan(&originalListenerId) | ||
if originalListenerId == "" { | ||
t.Errorf("the original Listener was not recreated") | ||
} | ||
|
||
// Simulate an errListenerClosed and wait for the listener to be recreated | ||
listener.Close() | ||
|
||
Eventually(func() error { | ||
var newListenerId string | ||
g2.Raw("SELECT pid FROM pg_stat_activity WHERE query LIKE 'LISTEN%channel%'").Scan(&newListenerId) | ||
if newListenerId == "" { | ||
return fmt.Errorf("the new Listener was not created") | ||
} | ||
// Validate the listener was re-created | ||
if originalListenerId == newListenerId { | ||
return fmt.Errorf("Listener was not re-created") | ||
} | ||
return nil | ||
}, 10*time.Second, 1*time.Second).Should(Succeed()) | ||
|
||
// send a notification to the new listener | ||
g2.Exec("NOTIFY channel, 'test'") | ||
|
||
// wait for the notification to be received | ||
time.Sleep(1 * time.Second) | ||
|
||
if <-result != "test" { | ||
t.Errorf("the notification was not received") | ||
} | ||
} |