Repository navigation
Expand file tree
/
Copy pathtest_error.py
More file actions
99 lines (72 loc) · 3.53 KB
/
Copy pathtest_error.py
File metadata and controls
99 lines (72 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2020 Confluent Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import pickle
from confluent_kafka import KafkaError
from confluent_kafka.error import ConsumeError, ProduceError
def test_new_consume_error_constant():
ce = ConsumeError(KafkaError(KafkaError._PARTITION_EOF))
assert ce.code == KafkaError._PARTITION_EOF
assert ce.name == u'_PARTITION_EOF'
def test_new_consume_error_caused_by():
ce = ConsumeError(KafkaError(KafkaError.INVALID_CONFIG), exception=ValueError())
assert ce.code == KafkaError.INVALID_CONFIG
assert ce.name == u'INVALID_CONFIG'
assert isinstance(ce.exception, ValueError)
def test_new_consume_error_custom_message():
ce = ConsumeError(KafkaError(KafkaError._KEY_SERIALIZATION, "Unable to serialize key"))
assert ce.code == KafkaError._KEY_SERIALIZATION
assert ce.name == u'_KEY_SERIALIZATION'
assert ce.args[0].str() == "Unable to serialize key"
def test_new_produce_error_constant():
pe = ProduceError(KafkaError(KafkaError._PARTITION_EOF))
assert pe.code == KafkaError._PARTITION_EOF
assert pe.name == u'_PARTITION_EOF'
def test_new_produce_error_caused_by():
pe = ProduceError(KafkaError(KafkaError.INVALID_CONFIG), exception=ValueError())
assert pe.code == KafkaError.INVALID_CONFIG
assert pe.name == u'INVALID_CONFIG'
assert isinstance(pe.exception, ValueError)
def test_new_produce_error_custom_message():
pe = ProduceError(KafkaError(KafkaError._KEY_SERIALIZATION, "Unable to serialize key"))
assert pe.code == KafkaError._KEY_SERIALIZATION
assert pe.name == u'_KEY_SERIALIZATION'
assert pe.args[0].str() == "Unable to serialize key"
def test_kafka_error_non_ascii_str():
# Covers the valid-UTF-8 non-ASCII path only: that the "replace" decode
# doesn't mangle legitimate non-English text. The public constructor can't
# inject invalid bytes, so the actual #448 invalid-byte case (reachable only
# from C or a genuinely non-UTF-8 locale) is not exercised here.
msg = u"Ошибка: недопустимое значение"
err = KafkaError(KafkaError._KEY_SERIALIZATION, msg)
assert err.str() == msg
assert msg in str(err)
assert repr(err)
def test_kafka_error_pickle():
# KafkaError_reduce() is what backs pickle and copy.deepcopy, and it
# decodes the reason string the same way KafkaError_str() does. Guards
# against a regression reintroducing #448 on the __reduce__ path, e.g. a
# KafkaError crossing a multiprocessing process boundary.
msg = u"Ошибка: недопустимое значение"
err = KafkaError(KafkaError._KEY_SERIALIZATION, msg, fatal=True, retriable=True, txn_requires_abort=False)
restored = pickle.loads(pickle.dumps(err))
assert restored.code() == err.code()
assert restored.str() == err.str()
assert restored.name() == err.name()
assert restored.fatal() == err.fatal()
assert restored.retriable() == err.retriable()
assert restored.txn_requires_abort() == err.txn_requires_abort()