pulsar-client 1.6.2 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ /**
21
+ * MIT License
22
+ *
23
+ * Copyright (c) 2020 Michael K
24
+ *
25
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
26
+ * of this software and associated documentation files (the "Software"), to deal
27
+ * in the Software without restriction, including without limitation the rights
28
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29
+ * copies of the Software, and to permit persons to whom the Software is
30
+ * furnished to do so, subject to the following conditions:
31
+ *
32
+ * The above copyright notice and this permission notice shall be included in all
33
+ * copies or substantial portions of the Software.
34
+ *
35
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41
+ * SOFTWARE.
42
+ *
43
+ *
44
+ * Adapted from https://github.com/0815fox/node-napi-threadsafe-deferred/tree/master/src
45
+ */
46
+
47
+ #include "ThreadSafeDeferred.h"
48
+
49
+ std::shared_ptr<ThreadSafeDeferred> ThreadSafeDeferred::New(const Napi::Env env) {
50
+ auto deferred = std::make_shared<ThreadSafeDeferred>(env);
51
+ deferred->self = deferred;
52
+ return deferred;
53
+ }
54
+
55
+ ThreadSafeDeferred::ThreadSafeDeferred(const Napi::Env env)
56
+ : Deferred(env),
57
+ fate(EFate::UNRESOLVED),
58
+ createValueCb(NULL),
59
+ errorMsg(""),
60
+ tsf{Napi::ThreadSafeFunction::New(env, Napi::Function::New(env, [](const Napi::CallbackInfo &info) {}),
61
+ "ThreadSafeDeferred", 0, 1, [this](Napi::Env env) {
62
+ // this access happens from another thread.
63
+ // However, no synchronization is needed as
64
+ // the other thread cannot modify this instance
65
+ // anymore after calling Resolve or Reject.
66
+ if (this->fate == EFate::RESOLVED) {
67
+ if (this->createValueCb == NULL) {
68
+ Napi::Promise::Deferred::Resolve(env.Undefined());
69
+ } else {
70
+ Napi::Promise::Deferred::Resolve(this->createValueCb(env));
71
+ }
72
+ } else {
73
+ Napi::Promise::Deferred::Reject(
74
+ Napi::Error::New(env, this->errorMsg).Value());
75
+ }
76
+
77
+ this->self.reset();
78
+ })} {}
79
+
80
+ void ThreadSafeDeferred::Resolve() {
81
+ if (this->fate != EFate::UNRESOLVED) throw "Cannot resolve a promise which is not unresolved anymore.";
82
+ this->fate = EFate::RESOLVED;
83
+ this->tsf.Release();
84
+ }
85
+
86
+ void ThreadSafeDeferred::Resolve(const createValueCb_t createValueCb) {
87
+ if (this->fate != EFate::UNRESOLVED) throw "Cannot resolve a promise which is not unresolved anymore.";
88
+ this->createValueCb = createValueCb;
89
+ this->fate = EFate::RESOLVED;
90
+ this->tsf.Release();
91
+ }
92
+
93
+ void ThreadSafeDeferred::Reject(const std::string &errorMsg) {
94
+ if (this->fate != EFate::UNRESOLVED) throw "Cannot reject a promise which is not unresolved anymore.";
95
+ this->errorMsg = errorMsg;
96
+ this->fate = EFate::REJECTED;
97
+ this->tsf.Release();
98
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ /**
21
+ * MIT License
22
+ *
23
+ * Copyright (c) 2020 Michael K
24
+ *
25
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
26
+ * of this software and associated documentation files (the "Software"), to deal
27
+ * in the Software without restriction, including without limitation the rights
28
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29
+ * copies of the Software, and to permit persons to whom the Software is
30
+ * furnished to do so, subject to the following conditions:
31
+ *
32
+ * The above copyright notice and this permission notice shall be included in all
33
+ * copies or substantial portions of the Software.
34
+ *
35
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41
+ * SOFTWARE.
42
+ */
43
+
44
+ #ifndef __THREADSAFE_DEFERRED_HPP
45
+ #define __THREADSAFE_DEFERRED_HPP
46
+
47
+ #include <napi.h>
48
+ #include <functional>
49
+
50
+ #define THREADSAFE_DEFERRED_RESOLVER(result) [=](const Napi::Env env) { return result; }
51
+
52
+ typedef std::function<Napi::Value(const Napi::Env env)> createValueCb_t;
53
+
54
+ class ThreadSafeDeferred : public Napi::Promise::Deferred {
55
+ private:
56
+ enum class EFate
57
+ {
58
+ UNRESOLVED,
59
+ RESOLVED,
60
+ REJECTED
61
+ };
62
+ EFate fate;
63
+ createValueCb_t createValueCb;
64
+ std::string errorMsg;
65
+ Napi::ThreadSafeFunction tsf;
66
+ std::shared_ptr<ThreadSafeDeferred> self;
67
+
68
+ public:
69
+ ThreadSafeDeferred(const Napi::Env env);
70
+
71
+ void Resolve(); // <- if only Resolve were virtual... But we can live without polymorphism here
72
+ void Resolve(const createValueCb_t);
73
+ inline void Reject() { this->Reject(""); }
74
+ void Reject(
75
+ const std::string &); // <- if only Reject were virtual... But we can live without polymorphism here
76
+
77
+ static std::shared_ptr<ThreadSafeDeferred> New(const Napi::Env env);
78
+ };
79
+
80
+ struct ExtDeferredContext {
81
+ ExtDeferredContext(std::shared_ptr<ThreadSafeDeferred> deferred) : deferred(deferred){};
82
+ std::shared_ptr<ThreadSafeDeferred> deferred;
83
+ };
84
+
85
+ #endif /* __THREADSAFE_DEFERRED_HPP */
@@ -102,7 +102,7 @@ const Pulsar = require('../index.js');
102
102
  subscription: 'sub1',
103
103
  ackTimeoutMs: 10000,
104
104
  nAckRedeliverTimeoutMs: 60000,
105
- })).rejects.toThrow('Failed to create consumer: ConnectError');
105
+ })).rejects.toThrow('Failed to create consumer: BrokerMetadataError');
106
106
  });
107
107
 
108
108
  test('Not Exist Namespace', async () => {
@@ -111,7 +111,7 @@ const Pulsar = require('../index.js');
111
111
  subscription: 'sub1',
112
112
  ackTimeoutMs: 10000,
113
113
  nAckRedeliverTimeoutMs: 60000,
114
- })).rejects.toThrow('Failed to create consumer: ConnectError');
114
+ })).rejects.toThrow('Failed to create consumer: BrokerMetadataError');
115
115
  });
116
116
 
117
117
  test('Not Positive NAckRedeliverTimeout', async () => {
@@ -106,7 +106,7 @@ const Pulsar = require('../index.js');
106
106
  consumer.acknowledge(msg2);
107
107
 
108
108
  await expect(consumer.receive(1000)).rejects.toThrow(
109
- 'Failed to received message TimeOut',
109
+ 'Failed to receive message: TimeOut',
110
110
  );
111
111
 
112
112
  expect(results).toEqual([message, message]);
@@ -302,7 +302,7 @@ const Pulsar = require('../index.js');
302
302
  }
303
303
 
304
304
  await expect(consumer.receive(1000)).rejects.toThrow(
305
- 'Failed to received message TimeOut',
305
+ 'Failed to receive message: TimeOut',
306
306
  );
307
307
 
308
308
  await producer.close();
@@ -55,7 +55,7 @@ const Pulsar = require('../index.js');
55
55
  topic: 'persistent://no-tenant/namespace/topic',
56
56
  sendTimeoutMs: 30000,
57
57
  batchingEnabled: true,
58
- })).rejects.toThrow('Failed to create producer: ConnectError');
58
+ })).rejects.toThrow('Failed to create producer: BrokerMetadataError');
59
59
  });
60
60
 
61
61
  test('Not Exist Namespace', async () => {
@@ -63,7 +63,7 @@ const Pulsar = require('../index.js');
63
63
  topic: 'persistent://public/no-namespace/topic',
64
64
  sendTimeoutMs: 30000,
65
65
  batchingEnabled: true,
66
- })).rejects.toThrow('Failed to create producer: ConnectError');
66
+ })).rejects.toThrow('Failed to create producer: BrokerMetadataError');
67
67
  });
68
68
 
69
69
  test('Automatic Producer Name', async () => {