Skip to content

Commit

Permalink
more run stuff into the shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf committed Mar 26, 2024
1 parent 27d6817 commit 76fcf24
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 50 deletions.
51 changes: 1 addition & 50 deletions examples/auth/callout/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,8 @@ public static void main(String[] args) throws Exception {
// ----------------------------------------------------------------------------------------------------
CompletableFuture<Boolean> serviceStoppedFuture = acService.startService();

test(natsURL, "alice", "alice", "test", "should connect, publish and receive");
test(natsURL, "alice", "wrong", "n/a", "should not connect");
test(natsURL, "bob", "bob", "bob.test", "should connect, publish and receive");
test(natsURL, "bob", "bob", "test", "should connect, publish but not receive");

// plenty of time to finish running the main.sh example script
Thread.sleep(2000);
Thread.sleep(20000);

// A real service will do something like this
// serviceStoppedFuture.get();
Expand All @@ -68,49 +63,5 @@ public static void main(String[] args) throws Exception {
e.printStackTrace();
}
}

public static void test(String natsURL, String u, String p, String subject, String behavior) {
System.out.println("\n--------------------------------------------------------------------------------");
System.out.println("[TEST] user : " + u);
System.out.println("[TEST] subject : " + subject);
System.out.println("[TEST] behavior : " + behavior);

Options options = new Options.Builder()
.server(natsURL)
.errorListener(new ErrorListener() {})
.userInfo(u, p)
.maxReconnects(3)
.build();

boolean connected = false;
try (Connection nc = Nats.connect(options)) {
System.out.println("[TEST] connected " + u);
connected = true;

AtomicBoolean gotMessage = new AtomicBoolean(false);
Dispatcher d = nc.createDispatcher(m -> {
System.out.println("[TEST] received message on '" + m.getSubject() + "'");
gotMessage.set(true);
});
d.subscribe(subject);

nc.publish(subject, (u + "-publish-" + System.currentTimeMillis()).getBytes());
System.out.println("[TEST] published to '" + subject + "'");

Thread.sleep(1000); // just giving time for publish to work

if (!gotMessage.get()) {
System.out.println("[TEST] no message from '" + subject + "'");
}
}
catch (Exception e) {
if (connected) {
System.out.println("[TEST] post connection exception, " + e);
}
else {
System.out.println("[TEST] did not connect, " + e);
}
}
}
}

23 changes: 23 additions & 0 deletions examples/auth/callout/java/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,26 @@ sleep 1
# Start the auth callout service providing the auth credentials
# to connect and the issuer seed.
java -cp /app/example.jar example.Main &

sleep 2

# Users sys, alice and bob are known to the example code
# Confirm alice can connect and publish.
echo -e '\n\n--------------------------------------------------\nTest alice'
nats --user alice --password alice pub test 'hello from alice'

# Confirm bob cannot publish to a subject they are not allowed to.
echo -e '\n\n--------------------------------------------------\nTest bob on denied subject'
nats --user bob --password bob pub test 'hello from bob' || true

# Confirm bob can connect and publish to a subject they are allowed to.
echo -e '\n\n--------------------------------------------------\nTest bob on allowed subject'
nats --user bob --password bob pub bob.test 'hello from bob'

# Confirm an unknown user cannot connect.
echo -e '\n\n--------------------------------------------------\nTest unknown user'
nats --user pam --password pam pub test 'hello from pam' || true

# Confirm the system account user has the expected system permission.
echo -e '\n\n--------------------------------------------------\nTest system account user'
nats --user sys --password sys server list

1 comment on commit 76fcf24

@github-actions
Copy link

Choose a reason for hiding this comment

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

Deploy preview for nats-by-example ready!

✅ Preview
https://nats-by-example-inasy05ch-connecteverything.vercel.app

Built with commit 76fcf24.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.