How to use NodeRemovedEvent class of org.openqa.selenium.grid.data package

Best Selenium code snippet using org.openqa.selenium.grid.data.NodeRemovedEvent

Source:GridModel.java Github

copy

Full Screen

...20import org.openqa.selenium.grid.data.Availability;21import org.openqa.selenium.grid.data.NodeDrainComplete;22import org.openqa.selenium.grid.data.NodeDrainStarted;23import org.openqa.selenium.grid.data.NodeId;24import org.openqa.selenium.grid.data.NodeRemovedEvent;25import org.openqa.selenium.grid.data.NodeStatus;26import org.openqa.selenium.grid.data.NodeStatusEvent;27import org.openqa.selenium.grid.data.Session;28import org.openqa.selenium.grid.data.SessionClosedEvent;29import org.openqa.selenium.grid.data.Slot;30import org.openqa.selenium.grid.data.SlotId;31import org.openqa.selenium.grid.security.Secret;32import org.openqa.selenium.internal.Require;33import org.openqa.selenium.remote.SessionId;34import java.time.Instant;35import java.util.HashSet;36import java.util.Iterator;37import java.util.Map;38import java.util.Optional;39import java.util.Set;40import java.util.concurrent.ConcurrentHashMap;41import java.util.concurrent.locks.Lock;42import java.util.concurrent.locks.ReadWriteLock;43import java.util.concurrent.locks.ReentrantReadWriteLock;44import java.util.logging.Logger;45import static org.openqa.selenium.grid.data.Availability.DOWN;46import static org.openqa.selenium.grid.data.Availability.DRAINING;47import static org.openqa.selenium.grid.data.Availability.UP;48public class GridModel {49 private static final Logger LOG = Logger.getLogger(GridModel.class.getName());50 private static final SessionId RESERVED = new SessionId("reserved");51 private final ReadWriteLock lock = new ReentrantReadWriteLock(/* fair */ true);52 private final Map<Availability, Set<NodeStatus>> nodes = new ConcurrentHashMap<>();53 private final EventBus events;54 public GridModel(EventBus events, Secret registrationSecret) {55 this.events = Require.nonNull("Event bus", events);56 Require.nonNull("Registration secret", registrationSecret);57 events.addListener(NodeDrainStarted.listener(nodeId -> setAvailability(nodeId, DRAINING)));58 events.addListener(NodeDrainComplete.listener(this::remove));59 events.addListener(NodeRemovedEvent.listener(this::remove));60 events.addListener(NodeStatusEvent.listener(status -> refresh(status)));61 events.addListener(SessionClosedEvent.listener(this::release));62 }63 public GridModel add(NodeStatus node) {64 Require.nonNull("Node", node);65 Lock writeLock = lock.writeLock();66 writeLock.lock();67 try {68 // If we've already added the node, remove it.69 for (Set<NodeStatus> nodes : nodes.values()) {70 Iterator<NodeStatus> iterator = nodes.iterator();71 while (iterator.hasNext()) {72 NodeStatus next = iterator.next();73 // If the ID is the same, we're re-adding a node. If the URI is the same a node probably restarted...

Full Screen

Full Screen

Source:LocalDistributor.java Github

copy

Full Screen

...28import org.openqa.selenium.grid.data.DistributorStatus;29import org.openqa.selenium.grid.data.NodeAddedEvent;30import org.openqa.selenium.grid.data.NodeDrainComplete;31import org.openqa.selenium.grid.data.NodeId;32import org.openqa.selenium.grid.data.NodeRemovedEvent;33import org.openqa.selenium.grid.data.NodeStatus;34import org.openqa.selenium.grid.data.NodeStatusEvent;35import org.openqa.selenium.grid.data.Slot;36import org.openqa.selenium.grid.data.SlotId;37import org.openqa.selenium.grid.distributor.Distributor;38import org.openqa.selenium.grid.distributor.selector.DefaultSlotSelector;39import org.openqa.selenium.grid.log.LoggingOptions;40import org.openqa.selenium.grid.node.HealthCheck;41import org.openqa.selenium.grid.node.Node;42import org.openqa.selenium.grid.node.remote.RemoteNode;43import org.openqa.selenium.grid.security.Secret;44import org.openqa.selenium.grid.server.BaseServerOptions;45import org.openqa.selenium.grid.server.EventBusOptions;46import org.openqa.selenium.grid.server.NetworkOptions;47import org.openqa.selenium.grid.sessionmap.SessionMap;48import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;49import org.openqa.selenium.internal.Require;50import org.openqa.selenium.remote.http.HttpClient;51import org.openqa.selenium.remote.tracing.Tracer;52import org.openqa.selenium.status.HasReadyState;53import java.time.Duration;54import java.util.ArrayList;55import java.util.HashMap;56import java.util.List;57import java.util.Map;58import java.util.Optional;59import java.util.Set;60import java.util.concurrent.locks.Lock;61import java.util.concurrent.locks.ReadWriteLock;62import java.util.concurrent.locks.ReentrantReadWriteLock;63import java.util.function.Supplier;64import java.util.logging.Level;65import java.util.logging.Logger;66import static com.google.common.collect.ImmutableSet.toImmutableSet;67import static org.openqa.selenium.grid.data.Availability.DOWN;68import static org.openqa.selenium.grid.data.Availability.DRAINING;69public class LocalDistributor extends Distributor {70 private static final Logger LOG = Logger.getLogger(LocalDistributor.class.getName());71 private final Tracer tracer;72 private final EventBus bus;73 private final HttpClient.Factory clientFactory;74 private final SessionMap sessions;75 private final Secret registrationSecret;76 private final Regularly hostChecker = new Regularly("distributor host checker");77 private final Map<NodeId, Runnable> allChecks = new HashMap<>();78 private final ReadWriteLock lock = new ReentrantReadWriteLock(/* fair */ true);79 private final GridModel model;80 private final Map<NodeId, Node> nodes;81 public LocalDistributor(82 Tracer tracer,83 EventBus bus,84 HttpClient.Factory clientFactory,85 SessionMap sessions,86 Secret registrationSecret) {87 super(tracer, clientFactory, new DefaultSlotSelector(), sessions, registrationSecret);88 this.tracer = Require.nonNull("Tracer", tracer);89 this.bus = Require.nonNull("Event bus", bus);90 this.clientFactory = Require.nonNull("HTTP client factory", clientFactory);91 this.sessions = Require.nonNull("Session map", sessions);92 this.model = new GridModel(bus, registrationSecret);93 this.nodes = new HashMap<>();94 this.registrationSecret = Require.nonNull("Registration secret", registrationSecret);95 bus.addListener(NodeStatusEvent.listener(this::register));96 bus.addListener(NodeStatusEvent.listener(model::refresh));97 bus.addListener(NodeDrainComplete.listener(this::remove));98 }99 public static Distributor create(Config config) {100 Tracer tracer = new LoggingOptions(config).getTracer();101 EventBus bus = new EventBusOptions(config).getEventBus();102 HttpClient.Factory clientFactory = new NetworkOptions(config).getHttpClientFactory(tracer);103 SessionMap sessions = new SessionMapOptions(config).getSessionMap();104 BaseServerOptions serverOptions = new BaseServerOptions(config);105 return new LocalDistributor(tracer, bus, clientFactory, sessions, serverOptions.getRegistrationSecret());106 }107 @Override108 public boolean isReady() {109 try {110 return ImmutableSet.of(bus, sessions).parallelStream()111 .map(HasReadyState::isReady)112 .reduce(true, Boolean::logicalAnd);113 } catch (RuntimeException e) {114 return false;115 }116 }117 private void register(NodeStatus status) {118 Require.nonNull("Node", status);119 Lock writeLock = lock.writeLock();120 writeLock.lock();121 try {122 if (nodes.containsKey(status.getId())) {123 return;124 }125 Set<Capabilities> capabilities = status.getSlots().stream()126 .map(Slot::getStereotype)127 .map(ImmutableCapabilities::copyOf)128 .collect(toImmutableSet());129 // A new node! Add this as a remote node, since we've not called add130 RemoteNode remoteNode = new RemoteNode(131 tracer,132 clientFactory,133 status.getId(),134 status.getUri(),135 registrationSecret,136 capabilities);137 add(remoteNode);138 } finally {139 writeLock.unlock();140 }141 }142 @Override143 public LocalDistributor add(Node node) {144 Require.nonNull("Node", node);145 LOG.info(String.format("Added node %s at %s.", node.getId(), node.getUri()));146 nodes.put(node.getId(), node);147 model.add(node.getStatus());148 // Extract the health check149 Runnable runnableHealthCheck = asRunnableHealthCheck(node);150 allChecks.put(node.getId(), runnableHealthCheck);151 hostChecker.submit(runnableHealthCheck, Duration.ofMinutes(5), Duration.ofSeconds(30));152 bus.fire(new NodeAddedEvent(node.getId()));153 return this;154 }155 private Runnable asRunnableHealthCheck(Node node) {156 HealthCheck healthCheck = node.getHealthCheck();157 NodeId id = node.getId();158 return () -> {159 HealthCheck.Result result;160 try {161 result = healthCheck.check();162 } catch (Exception e) {163 LOG.log(Level.WARNING, "Unable to process node " + id, e);164 result = new HealthCheck.Result(DOWN, "Unable to run healthcheck. Assuming down");165 }166 Lock writeLock = lock.writeLock();167 writeLock.lock();168 try {169 model.setAvailability(id, result.getAvailability());170 } finally {171 writeLock.unlock();172 }173 };174 }175 @Override176 public boolean drain(NodeId nodeId) {177 Node node = nodes.get(nodeId);178 if (node == null) {179 LOG.info("Asked to drain unregistered node " + nodeId);180 return false;181 }182 Lock writeLock = lock.writeLock();183 writeLock.lock();184 try {185 node.drain();186 model.setAvailability(nodeId, DRAINING);187 } finally {188 writeLock.unlock();189 }190 return node.isDraining();191 }192 public void remove(NodeId nodeId) {193 Lock writeLock = lock.writeLock();194 writeLock.lock();195 try {196 model.remove(nodeId);197 Runnable runnable = allChecks.remove(nodeId);198 if (runnable != null) {199 hostChecker.remove(runnable);200 }201 } finally {202 writeLock.unlock();203 bus.fire(new NodeRemovedEvent(nodeId));204 }205 }206 @Override207 public DistributorStatus getStatus() {208 Lock readLock = this.lock.readLock();209 readLock.lock();210 try {211 return new DistributorStatus(model.getSnapshot());212 } finally {213 readLock.unlock();214 }215 }216 @Beta217 public void refresh() {...

Full Screen

Full Screen

Source:LocalSessionMap.java Github

copy

Full Screen

...17package org.openqa.selenium.grid.sessionmap.local;18import org.openqa.selenium.NoSuchSessionException;19import org.openqa.selenium.events.EventBus;20import org.openqa.selenium.grid.config.Config;21import org.openqa.selenium.grid.data.NodeRemovedEvent;22import org.openqa.selenium.grid.data.NodeRestartedEvent;23import org.openqa.selenium.grid.data.Session;24import org.openqa.selenium.grid.data.SessionClosedEvent;25import org.openqa.selenium.grid.log.LoggingOptions;26import org.openqa.selenium.grid.server.EventBusOptions;27import org.openqa.selenium.grid.sessionmap.SessionMap;28import org.openqa.selenium.internal.Require;29import org.openqa.selenium.remote.SessionId;30import org.openqa.selenium.remote.tracing.AttributeKey;31import org.openqa.selenium.remote.tracing.EventAttribute;32import org.openqa.selenium.remote.tracing.EventAttributeValue;33import org.openqa.selenium.remote.tracing.Span;34import org.openqa.selenium.remote.tracing.Tracer;35import java.util.HashMap;36import java.util.Map;37import java.util.concurrent.ConcurrentHashMap;38import java.util.concurrent.locks.Lock;39import java.util.concurrent.locks.ReadWriteLock;40import java.util.concurrent.locks.ReentrantReadWriteLock;41import java.util.logging.Logger;42import static org.openqa.selenium.remote.RemoteTags.SESSION_ID;43import static org.openqa.selenium.remote.RemoteTags.SESSION_ID_EVENT;44public class LocalSessionMap extends SessionMap {45 private static final Logger LOG = Logger.getLogger(LocalSessionMap.class.getName());46 private final EventBus bus;47 private final Map<SessionId, Session> knownSessions = new ConcurrentHashMap<>();48 private final ReadWriteLock lock = new ReentrantReadWriteLock(/* be fair */ true);49 public LocalSessionMap(Tracer tracer, EventBus bus) {50 super(tracer);51 this.bus = Require.nonNull("Event bus", bus);52 bus.addListener(SessionClosedEvent.listener(id -> {53 try (Span span = tracer.getCurrentContext().createSpan("local_sessionmap.remove")) {54 Map<String, EventAttributeValue> attributeMap = new HashMap<>();55 attributeMap.put(AttributeKey.LOGGER_CLASS.getKey(),56 EventAttribute.setValue(getClass().getName()));57 SESSION_ID.accept(span, id);58 SESSION_ID_EVENT.accept(attributeMap, id);59 knownSessions.remove(id);60 String sessionDeletedMessage = "Deleted session from local session map";61 span.addEvent(sessionDeletedMessage, attributeMap);62 LOG.info(String.format("%s, Id: %s", sessionDeletedMessage, id));63 }64 }));65 bus.addListener(NodeRemovedEvent.listener(nodeStatus -> nodeStatus.getSlots().stream()66 .filter(slot -> slot.getSession() != null)67 .map(slot -> slot.getSession().getId())68 .forEach(knownSessions::remove)));69 bus.addListener(NodeRestartedEvent.listener(nodeStatus -> knownSessions.values()70 .removeIf(value -> value.getUri().equals(nodeStatus.getExternalUri()))));71 }72 public static SessionMap create(Config config) {73 Tracer tracer = new LoggingOptions(config).getTracer();74 EventBus bus = new EventBusOptions(config).getEventBus();75 return new LocalSessionMap(tracer, bus);76 }77 @Override78 public boolean isReady() {79 return bus.isReady();...

Full Screen

Full Screen

Source:NodeRemovedEvent.java Github

copy

Full Screen

...17package org.openqa.selenium.grid.data;18import org.openqa.selenium.events.Event;19import org.openqa.selenium.events.Type;20import java.util.UUID;21public class NodeRemovedEvent extends Event {22 public static final Type NODE_REMOVED = new Type("node-removed");23 public NodeRemovedEvent(UUID nodeId) {24 super(NODE_REMOVED, nodeId);25 }26}...

Full Screen

Full Screen

NodeRemovedEvent

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.grid.data.NodeRemovedEvent;2import org.openqa.selenium.remote.http.HttpRequest;3import org.openqa.selenium.remote.http.HttpResponse;4import org.openqa.selenium.remote.tracing.Tracer;5import org.openqa.selenium.remote.tracing.Span;6import org.openqa.selenium.remote.tracing.Span;7import org.openqa.selenium.remote.tracing.Tracer;8import java.io.IOException;9import java.time.Duration;10import java.util.Objects;11import java.util.concurrent.atomic.AtomicBoolean;12import java.util.logging.Logger;13import static org.openqa.selenium.remote.http.Contents.utf8String;14public class NodeRemovedEvent implements Event {15 private static final Logger LOG = Logger.getLogger(NodeRemovedEvent.class.getName());16 private final NodeId nodeId;17 private final long timestamp;18 private final String reason;19 public NodeRemovedEvent(NodeId nodeId, long timestamp, String reason) {20 this.nodeId = Objects.requireNonNull(nodeId);21 this.timestamp = timestamp;22 this.reason = Objects.requireNonNull(reason);23 }24 public NodeId getNodeId() {25 return nodeId;26 }27 public long getTimestamp() {28 return timestamp;29 }30 public String getReason() {31 return reason;32 }33 public String toString() {34 return String.format("Node %s removed at %d: %s", nodeId, timestamp, reason);35 }36 public static class Codec implements EventCodec<NodeRemovedEvent> {37 private final Tracer tracer;38 public Codec(Tracer tracer) {39 this.tracer = Objects.requireNonNull(tracer);40 }41 public NodeRemovedEvent decode(HttpRequest req, HttpResponse resp) throws IOException {42 String nodeId = req.getQueryParameter("nodeId");43 long timestamp = Long.parseLong(req.getQueryParameter("timestamp"));44 String reason = req.getQueryParameter("reason");45 return new NodeRemovedEvent(new NodeId(nodeId), timestamp, reason);46 }47 public void encode(NodeRemovedEvent event, HttpRequest req, HttpResponse resp) {48 req.setQueryParameter("nodeId", event.getNodeId().toString());49 req.setQueryParameter("timestamp", Long.toString(event.getTimestamp()));50 req.setQueryParameter("reason", event.getReason());51 }52 }53 public static class Handler implements EventHandler {54 private final Tracer tracer;55 private final NodeRegistry registry;56 private final AtomicBoolean isShutdown;57 public Handler(Tracer tracer, NodeRegistry registry) {58 this.tracer = Objects.requireNonNull(tracer

Full Screen

Full Screen

NodeRemovedEvent

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.grid.data.NodeRemovedEvent;2import org.openqa.selenium.grid.data.NodeId;3import org.openqa.selenium.remote.http.HttpClient;4import org.openqa.selenium.remote.http.HttpRequest;5import org.openqa.selenium.remote.http.HttpResponse;6import org.openqa.selenium.remote.tracing.Tracer;7import org.openqa.selenium.remote.tracing.Span;8import org.openqa.selenium.remote.tracing.Span.SpanBuilder;9import org.openqa.selenium.remote.tracing.Tracer;10import org.openqa.selenium.remote.tracing.TracerBuilder;11import org.openqa.selenium.remote.tracing.HttpTracing;12import org.openqa.selenium.remote.tracing.HttpTracingBuilder;13import org.openqa.selenium.remote.tracing.HttpTracingBuilder;14import org.openqa.selenium.remote.tracing.HttpTracingBuilder;15import java.io.IOException;16import java.net.URI;17import java.util.Map;18import java.util.Objects;19import java.util.concurrent.atomic.AtomicReference;20import java.util.logging.Logger;21import static org.openqa.selenium.remote.http.Contents.string;22public class NodeRemovedEventTest {23 public static void main(String[] args) {24 Tracer tracer = TracerBuilder.createDefault().build();25 HttpTracing tracing = HttpTracingBuilder.createDefault().build();26 HttpRequest request = new HttpRequest(HttpMethod.GET, "/status");27 HttpResponse response = client.execute(request, tracing);28 System.out.println(string(response));29 }30}31import org.openqa.selenium.grid.data.NodeRemovedEvent;32import org.openqa.selenium.grid.data.NodeId;33import org.openqa.selenium.remote.http.HttpClient;34import org.openqa.selenium.remote.http.HttpRequest;35import org.openqa.selenium.remote.http.HttpResponse;36import org.openqa.selenium.remote.tracing.Tracer;37import org.openqa.selenium.remote.tracing.Span;38import org.openqa.selenium.remote.tracing.Span.SpanBuilder;39import org.openqa.selenium.remote.tracing.Tracer;40import org.openqa.selenium.remote.tracing.TracerBuilder;41import org.openqa.selenium.remote.tracing.HttpTracing;42import org.openqa.selenium.remote.tracing.HttpTracingBuilder;43import org.openqa.selenium.remote.tracing.HttpTracingBuilder;44import org.openqa.selenium.remote.tracing.HttpTracingBuilder;45import java.io.IOException;46import java.net.URI;47import java.util.Map;48import java.util.Objects;49import java.util.concurrent.atomic.AtomicReference;50import java.util.logging.Logger;51import static org.openqa.selenium.remote.http.Contents.string;

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in NodeRemovedEvent

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful