Exemple #1
0
    def get_or_create(self, url):
        if url not in self.sessions:
            conn = nrepl.connect(url)
            wconn = nrepl.WatchableConnection(conn)

            self.sessions[url] = {'conn': wconn}

        return self.sessions[url]['conn']
Exemple #2
0
    def get_or_create(self, url):
        if url not in self.sessions:
            conn = nrepl.connect(url)
            wconn = nrepl.WatchableConnection(conn)
            if self.default_handler is not None:
                watcher_key = "{}-watcher".format(uuid.uuid4().hex)
                wconn.watch(watcher_key, {}, self.default_handler)
                self.default_handler({"wc": watcher_key}, None, None)

            self.sessions[url] = {'conn': wconn}

        return self.sessions[url]['conn']
Exemple #3
0
    def get_conn(self, conn_string):
        if conn_string not in self.__conns:
            conn = nrepl.connect(conn_string)

            def global_watch(cmsg, cwc, ckey):
                self.logger.debug(
                    "Received message for {}".format(conn_string))
                self.logger.debug(cmsg)

            wc = nrepl.WatchableConnection(conn)
            self.__conns[conn_string] = wc
            wc.watch("global_watch", {}, global_watch)

        return self.__conns.get(conn_string)
Exemple #4
0
    def get_or_create(self, url):
        if url not in self.sessions:
            conn = nrepl.connect(url)
            wconn = nrepl.WatchableConnection(conn)

            self.sessions[url] = {'conn': wconn}

            def clone_handler(msg, wc, key):
                self.sessions[key]['session'] = msg['new-session']
                wc.unwatch(key)

            wconn.watch(url, {'new-session': None}, clone_handler)
            wconn.send({'op': 'clone'})

        return self.sessions[url]['conn']
Exemple #5
0
    def test_async_watches(self):
        c = nrepl.connect("nrepl://localhost:" + self.port)
        wc = nrepl.WatchableConnection(c)
        outs = {}

        def add_resp(session, msg):
            out = msg.get("out", None)
            if out: outs[session].append(out)

        def watch_new_sessions(msg, wc, key):
            session = msg.get("new-session")
            outs[session] = []
            wc.watch("session" + session, {"session": session},
                     lambda msg, wc, key: add_resp(session, msg))

        wc.watch("sessions", {"new-session": None}, watch_new_sessions)
        wc.send({"op": "clone"})
        wc.send({"op": "clone"})
        time.sleep(0.5)
        for i, session in enumerate(outs.keys()):
            wc.send({
                "op":
                "eval",
                "session":
                session,
                "code":
                """(do (future (Thread/sleep %s00)
                (println %s)
                (println (System/currentTimeMillis))))""" % (i, i)
            })
        time.sleep(2)
        for i, (session, _outs) in enumerate(outs.items()):
            self.assertEqual(i, int(_outs[0]))
        # Python3 got dicts that we cant slice, thus we wrap it in a list.
        outs_values = list(outs.values())
        self.assertTrue(int(outs_values[0][1]) < int(outs_values[1][1]))