Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry in templates to wait realtime notifications #161

Merged
merged 45 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
a677a3d
[realtime tests] add sleep to wait notifications
Aschen Nov 23, 2018
0b0a6cf
[realtime tests] extract sleep from the code snippets
Aschen Nov 23, 2018
21bf4a7
[realtime tests] remove sleep in cpp snippet
Aschen Nov 23, 2018
e32e360
[realtime tests] increase sleep for js
Aschen Nov 23, 2018
91857f3
[realtime tests] use retry in cpp template
Aschen Nov 23, 2018
4cdc08a
[realtime tests] use retry in java template
Aschen Nov 23, 2018
24a87c5
[realtime tests] use retry in go template
Aschen Nov 23, 2018
46b7010
[realtime tests] use retry in js template
Aschen Nov 23, 2018
720997d
[realtime tests] nitpicking
Aschen Nov 23, 2018
706ea6c
[realtime tests] prevent infinite loops
Aschen Nov 23, 2018
cb9253a
[realtime tests] exit with non-zero code when timeout in go
Aschen Nov 23, 2018
9639cd2
[realtime tests] add sleep to wait notifications
Aschen Nov 23, 2018
35b338d
[realtime tests] extract sleep from the code snippets
Aschen Nov 23, 2018
1199b28
[realtime tests] remove sleep in cpp snippet
Aschen Nov 23, 2018
6ca00a9
[realtime tests] increase sleep for js
Aschen Nov 23, 2018
a555b16
[realtime tests] use retry in cpp template
Aschen Nov 23, 2018
89700bf
[realtime tests] use retry in java template
Aschen Nov 23, 2018
948490f
[realtime tests] use retry in go template
Aschen Nov 23, 2018
37efd20
[realtime tests] use retry in js template
Aschen Nov 23, 2018
007bed9
[realtime tests] nitpicking
Aschen Nov 23, 2018
41d634c
[realtime tests] prevent infinite loops
Aschen Nov 23, 2018
7cd127d
[realtime tests] exit with non-zero code when timeout in go
Aschen Nov 23, 2018
8ea341f
Merge branch 'fix-realtime-tests' of github.com:kuzzleio/documentatio…
Aschen Nov 27, 2018
4b5e07e
[realtime tests] fix js realtime
Aschen Nov 27, 2018
c9ebf6d
[realtime tests] fix js realtime
Aschen Nov 27, 2018
851e07f
[c++] update templates to the latest SDK version
scottinet Nov 28, 2018
c875266
[templates] C++: kuzzle->connect now throws
scottinet Nov 28, 2018
0d19c20
[cleanup] revert type change
scottinet Nov 28, 2018
e944bba
[c++] upgrade SDK install to the latest changes
scottinet Nov 28, 2018
2a8dcf9
Merge remote-tracking branch 'origin/upgrade-cpp-templates' into fix-…
scottinet Nov 28, 2018
c181328
Merge remote-tracking branch 'origin/master' into fix-realtime-tests
scottinet Nov 28, 2018
f6489d2
[c++] upgrade code snippets
scottinet Nov 28, 2018
154cce2
[java] upgrade templates to the new kuzzle constructor
scottinet Nov 28, 2018
6559596
Merge remote-tracking branch 'origin/upgrade-cpp-templates' into fix-…
scottinet Nov 28, 2018
94bbb9c
[realtime] upgrade templates to the latest kuzzle constructor
scottinet Nov 28, 2018
5a67369
[realtime] wait for 30s before throwing a timeout
scottinet Nov 28, 2018
9e64df3
[templates] realtime: timeout after 30s
scottinet Nov 29, 2018
de6e9d3
[realtime] JS template: exit even if the event loop is not empty
scottinet Nov 29, 2018
ea3e090
[realtime] JS: stricter tests + fix explanation
scottinet Nov 29, 2018
f20db37
[snippets] multi-snippets wrongly check column precedence
scottinet Nov 29, 2018
7645862
[realtime] stricter tests + snippet fixes
scottinet Nov 29, 2018
f8f8e20
[realtime] stabilize realtime tests
scottinet Nov 29, 2018
ee3b481
[templates] C++: getPort() is now only accessible through the Protoco…
scottinet Nov 30, 2018
3b3306c
Merge remote-tracking branch 'origin/master' into fix-realtime-tests
scottinet Nov 30, 2018
ee6d075
[java] fix user notifications snippet
scottinet Nov 30, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,028 changes: 1,792 additions & 2,236 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ try {
"yellow-taxi",
"nina-vkote",
"{ \"age\": 42 }");

sleep(1);
} catch (kuzzleio::KuzzleException &e) {
std::cerr << e.getMessage() << std::endl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ description: Subscribe to documents leaving the scope
hooks:
before: curl -X POST kuzzle:7512/nyc-open-data/_create ; curl -X PUT kuzzle:7512/nyc-open-data/yellow-taxi/; curl -X DELETE kuzzle:7512/nyc-open-data/yellow-taxi/nina-vkote
after:
template: default
template: realtime
expected: Document moved out from the scope
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ try {
kuzzle->document->create("nyc-open-data", "yellow-taxi", "nina-vkote", document);
} catch (kuzzleio::KuzzleException &e) {
std::cerr << e.getMessage() << std::endl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ description: Simple subscribe to document notifications
hooks:
before: curl -X POST kuzzle:7512/nyc-open-data/_create ; curl -X PUT kuzzle:7512/nyc-open-data/yellow-taxi/ ; curl -X DELETE kuzzle:7512/nyc-open-data/yellow-taxi/nina-vkote
after:
template: default
template: realtime
expected: Document nina-vkote enter the scope
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ try {

std::string message = "{ \"metAt\": \"Insane\", \"hello\": \"world\" }";
kuzzle->realtime->publish("i-dont-exist", "in-database", message);

sleep(1);
} catch (kuzzleio::KuzzleException &e) {
std::cerr << e.getMessage() << std::endl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ description: Subscribe to message notifications
hooks:
before:
after:
template: default
template: realtime
expected: Message notification received
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ try {
fuzzle->realtime->subscribe("nyc-open-data", "yellow-taxi", filters, &listener);
} catch (kuzzleio::KuzzleException &e) {
std::cerr << e.getMessage() << std::endl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ description: Subscribe to users joining or leaving the room
hooks:
before: curl -X POST kuzzle:7512/nyc-open-data/_create ; curl -X PUT kuzzle:7512/nyc-open-data/yellow-taxi/
after:
template: default
template: realtime
expected: Currently 2 users in the room
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Avoid program exit before receiving notification
exitPrgm := make(chan bool)

// Subscribe to notifications for documents containing a 'name' property
filters := json.RawMessage(`{ "range": { "age": { "lte": 20 } } }`)

Expand All @@ -10,8 +7,6 @@ go func() {
notification := <-listener

fmt.Printf("Document moved %s from the scope\n", notification.Scope)

exitPrgm <- true
}()

options := types.NewRoomOptions()
Expand Down Expand Up @@ -45,5 +40,3 @@ kuzzle.Document.Update(
"nina-vkote",
json.RawMessage(`{ "age": 42 }`),
nil)

<-exitPrgm
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Avoid program exit before receiving notification
exitPrgm := make(chan bool)

// Subscribe to notifications for documents containing a 'name' property
filters := json.RawMessage(`{ "exists": "name" }`)

Expand All @@ -14,8 +11,6 @@ go func() {
} else {
fmt.Printf("Document %s leave the scope\n", notification.Result.Id)
}

exitPrgm <- true
}()

_, err := kuzzle.Realtime.Subscribe(
Expand All @@ -36,5 +31,3 @@ kuzzle.Document.Create(
"nina-vkote",
document,
nil)

<-exitPrgm
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
// Avoid program exit before receiving notification
exitPrgm := make(chan bool)

// Start an async listener
listener := make(chan types.KuzzleNotification)
go func() {
<-listener

fmt.Printf("Message notification received")

exitPrgm <- true
}()

// Subscribe to a room
Expand All @@ -26,5 +21,3 @@ if err != nil {
message := json.RawMessage(`{ "metAt": "Insane", "hello": "world" }`)
// Publish a message to this room
kuzzle.Realtime.Publish("i-dont-exist", "i-database", message, nil)

<-exitPrgm
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Avoid program exit before receiving notification
exitPrgm := make(chan bool)

// Subscribe to notifications for documents containing a 'name' property
filters := json.RawMessage(`{ "exists": "name" }`)

Expand All @@ -14,8 +11,6 @@ go func() {
// Volatile data: {"sdkVersion":"1.0.0","username":"nina vkote"}
fmt.Printf("Currently %d users in the room\n", notification.Result.Count)
}

exitPrgm <- true
}()

options := types.NewRoomOptions()
Expand Down Expand Up @@ -53,5 +48,3 @@ fuzzle.Realtime.Subscribe(
filters,
make(chan types.KuzzleNotification),
nil)

<-exitPrgm
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,30 @@ public void onMessage(NotificationResult notification) {

// Subscribe to notifications when document leaves the scope
kuzzle.getRealtime().subscribe(
"nyc-open-data",
"yellow-taxi",
filters,
listener,
"nyc-open-data",
"yellow-taxi",
filters,
listener,
options
);

String document = "{ \"name\": \"nina vkote\", \"age\": 19 }";

// The document is in the scope
kuzzle.getDocument().create(
"nyc-open-data",
"yellow-taxi",
"nina-vkote",
"nyc-open-data",
"yellow-taxi",
"nina-vkote",
document
);

// The document isn't in the scope anymore
kuzzle.getDocument().update(
"nyc-open-data",
"yellow-taxi",
"nina-vkote",
"nyc-open-data",
"yellow-taxi",
"nina-vkote",
"{ \"age\": 42 }"
);

Thread.sleep(1000);
} catch (KuzzleException e) {
System.err.println(e.getMessage());
} catch (InterruptedException e) {
System.err.println(e.getMessage());
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ description: Subscribe to documents leaving the scope
hooks:
before: curl -X POST kuzzle:7512/nyc-open-data/_create ; curl -X PUT kuzzle:7512/nyc-open-data/yellow-taxi/; curl -X DELETE kuzzle:7512/nyc-open-data/yellow-taxi/nina-vkote
after:
template: default
template: realtime
expected: Document moved out from the scope
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public void onMessage(NotificationResult notification) {
"nina-vkote",
document
);

Thread.sleep(1000);
} catch (KuzzleException e) {
System.err.println(e.getMessage());
} catch (InterruptedException e) {
} catch (KuzzleException e) {
System.err.println(e.getMessage());
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ description: Simple subscribe to document notifications
hooks:
before: curl -X POST kuzzle:7512/nyc-open-data/_create ; curl -X PUT kuzzle:7512/nyc-open-data/yellow-taxi/; curl -X DELETE kuzzle:7512/nyc-open-data/yellow-taxi/nina-vkote
after:
template: default
template: realtime
expected: Document nina-vkote enter the scope
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public void onMessage(NotificationResult notification) {

try {
kuzzle.getRealtime().subscribe(
"i-dont-exist",
"in-database",
"{}",
"i-dont-exist",
"in-database",
"{}",
listener
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ description: Subscribe to message notifications
hooks:
before:
after:
template: default
template: realtime
expected: Message notification received
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public void onMessage(NotificationResult notification) {

try {
kuzzle.getRealtime().subscribe(
"nyc-open-data",
"yellow-taxi",
filters,
listener,
"nyc-open-data",
"yellow-taxi",
filters,
listener,
options
);

Expand All @@ -29,11 +29,11 @@ public void onMessage(NotificationResult notification) {

// Subscribe to the same room with the second client
fuzzle.getRealtime().subscribe(
"nyc-open-data",
"yellow-taxi",
filters,
"nyc-open-data",
"yellow-taxi",
filters,
listener
);
} catch (KuzzleException e) {
} catch (KuzzleException e) {
System.err.println(e.getMessage());
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ description: Subscribe to users joining or leaving the room
hooks:
before: curl -X POST kuzzle:7512/nyc-open-data/_create ; curl -X PUT kuzzle:7512/nyc-open-data/yellow-taxi/
after:
template: default
template: realtime
expected: Currently 2 users in the room
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ try {
await kuzzle.document.update('nyc-open-data', 'yellow-taxi', _id, { age: 42 });
} catch (error) {
console.log(error.message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ description: Simple subscribe to document notifications
hooks:
before: curl -X POST kuzzle:7512/nyc-open-data/_create ; curl -X PUT kuzzle:7512/nyc-open-data/yellow-taxi/
after:
template: default
template: realtime
expected: Document nina vkote enter the scope
2 changes: 1 addition & 1 deletion test/templates/catch.tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const kuzzle = new Kuzzle('websocket', {

// add a listener to detect any connection problems
kuzzle.on('networkError', error => {
console.error(`Network Error: ${error}`);
console.error(`Network Error: ${error.message}`);
});

(async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/templates/default.tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const kuzzle = new Kuzzle('websocket', {

// add a listener to detect any connection problems
kuzzle.on('networkError', error => {
console.error(`Network Error: ${error}`);
console.error(`Network Error: ${error.message}`);
});

(async () => {
Expand Down
54 changes: 54 additions & 0 deletions test/templates/realtime.tpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <unistd.h>

#include <sstream>
#include <iostream>
#include <string>
#include <ctime>
#include <vector>

#include "auth.hpp"
#include "collection.hpp"
#include "document.hpp"
#include "server.hpp"
#include "index.hpp"
#include "realtime.hpp"
#include "kuzzle.hpp"

bool is_empty(std::ostringstream *stream) {
bool empty;
std::streampos position = stream->tellp();

stream->seekp(0, std::ios_base::end);
empty = stream->tellp() == 0;
stream->seekp(position);

return empty;
}

int main() {
std::string hostname = "kuzzle";

kuzzleio::Kuzzle* kuzzle = new kuzzleio::Kuzzle(hostname);

char* error = kuzzle->connect();
if (error != nullptr) {
std::cerr << "Unable to connect to " << hostname << ":7512\n" << error << std::endl;
return 1;
}

std::streambuf *cout_original = std::cout.rdbuf();
std::ostringstream cout_copy;

std::cout.rdbuf(cout_copy.rdbuf());

[snippet-code]

for (size_t i = 20; i > 0 && is_empty(&cout_copy); --i) {
usleep(200);
}

std::cout.rdbuf(cout_original);
std::cout << cout_copy.str();

return 0;
}
17 changes: 13 additions & 4 deletions test/templates/realtime.tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@ func main() {
os.Exit(1)
}

rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

[snippet-code]

go func() {
time.Sleep(1 * time.Second)
fmt.Println("Timeout exceeded")
os.Exit(2)
time.Sleep(4 * time.Second)
os.Exit(1)
}()

[snippet-code]
b := make([]byte, 4096)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should make sure that the buffer has been completely emptied and printed before stopping the snippet: 4k may not be large enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pick this value that should be enough because in the snippets we always print a string with a fixed length

n, _ := r.Read(b)
scottinet marked this conversation as resolved.
Show resolved Hide resolved

os.Stdout = rescueStdout
fmt.Print(string(b[:n]))
}
Loading