예제 #1
0
def test_atomiclong_self_comparison_race():
    """
    When comparing an AtomicLong to itself, it is possible that
    self.value and other.value will not be equal because the underlying
    long may have been incremented by another thread during the comparison.

    Here we simulate this situation by ensuring that AtomicLong.value returns
    two different numbers during the comparisons, by swapping out the
    underlying storage for an object that pops items off a list.
    """

    class StubStorage(object):

        def __init__(self, values):
            self._values = values

        def __getitem__(self, _idx):
            return self._values.pop()

    l1 = AtomicLong(0)

    l1.store = StubStorage([0, 1])
    assert l1 == l1

    l1.store = StubStorage([0, 1])
    assert not (l1 < l1)
예제 #2
0
 def __init__(self, config=None, scale=DEFAULT_HIST_SCALE):
     self.scale = DEFAULT_HIST_SCALE
     self.labels = [str(x) for x in config] + ["+Inf"]
     self.thresholds = [int(x * scale) for x in config]
     self.metrics = [AtomicLong(0) for _ in range(len(config) + 1)]
     self.total_sum = AtomicLong(0)
     self.total_count = AtomicLong(0)
예제 #3
0
def test_atomiclong_eq():
    l1 = AtomicLong(0)
    l2 = AtomicLong(1)
    l3 = AtomicLong(0)
    assert l1 == 0
    assert l1 != 1
    assert not (l2 == 0)
    assert not (l2 != 1)
    assert l1 == l3
    assert not (l1 != l3)
    assert l1 != l2
    assert not (l1 == l2)
예제 #4
0
def test_atomiclong_ordering():
    l1 = AtomicLong(0)
    l2 = AtomicLong(1)
    l3 = AtomicLong(0)

    assert l1 < l2
    assert l1 <= l2
    assert l1 <= l3
    assert l2 > l1
    assert l2 >= l3
    assert l2 >= l2

    assert l1 < 1
    assert l1 <= 0
    assert l1 <= 1
    assert l1 > -1
    assert l1 >= -1
    assert l1 >= 0
예제 #5
0
    def __init__(self,
                 master_inbox_path=DEFAULT_MASTER_INBOX,
                 container_inboxes_path=DEFAULT_CONTAINER_INBOXES,
                 atomic_counter=AtomicLong(0)):
        self.master_inbox_path = master_inbox_path
        self.container_inboxes_path = container_inboxes_path
        self.atomic_counter = atomic_counter
        self.event_subscriptions = {
        }  # Keep callbacks for events subscribed to
        self.count_cache = {
        }  # Keep a cache of file counts; faster than recounting

        if not os.path.exists(self.master_inbox_path):
            os.makedirs(self.master_inbox_path)

        if not os.path.exists(self.container_inboxes_path):
            os.makedirs(self.container_inboxes_path)
예제 #6
0
class TimeStamp:
    """Class to implement timestamp"""

    _time_stamp = AtomicLong(0)

    ##############################################

    def __init__(self):
        self._modified_time = 0

    ##############################################

    def __repr__(self):
        return 'TS {}'.format(self._modified_time)

    ##############################################

    def __lt__(self, other):
        return self._modified_time < other.modified_time

    ##############################################

    def __gt__(self, other):
        return self._modified_time > other.modified_time

    ##############################################

    def __int__(self):
        return self._modified_time

    ##############################################

    def modified(self):

        # Should be atomic
        TimeStamp._time_stamp += 1
        self._modified_time = TimeStamp._time_stamp.value
예제 #7
0
    def __init__(self, start_id=0):

        self._id = AtomicLong(start_id)
예제 #8
0
# 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 threading

from atomiclong import AtomicLong

syncing = threading.Event()
sync_done = threading.Event()

push_successful = threading.Event()
push_successful.set()

fetch_successful = threading.Event()
fetch_successful.set()

read_only = threading.Event()
fetch = threading.Event()
shutting_down = threading.Event()

writers = AtomicLong(0)

remote_operation = threading.Lock()
예제 #9
0
   ...
   from noc.core.perf import metrics

   metrics["my_metric1"] += 1
   metrics["my_metric2"] = 2
"""
# Python modules
from collections import defaultdict
# Third-party modules
from atomiclong import AtomicLong
import six

#
# Performance metrics
#
metrics = defaultdict(lambda: AtomicLong(0))


def apply_metrics(d):
    """
    Apply metrics value to dictionary d
    :param d: Dictionary
    :return:
    """
    for k, v in six.iteritems(metrics):
        if isinstance(v, AtomicLong):
            v = v.value
        d[k] = v
    return d
예제 #10
0
파일: event.py 프로젝트: KrisSiegel/bakula
from bakula.docker.dockeragent import DockerAgent
from bakula.events.inboxer import Inboxer, DEFAULT_CONTAINER_INBOXES, DEFAULT_MASTER_INBOX
from bakula.events.orchestrator import Orchestrator
from atomiclong import AtomicLong
from bakula.security.tokenauthplugin import TokenAuthorizationPlugin

app = Bottle()

configuration.bootstrap_app_config(app)

# Setup authorization plugin
token_secret = app.config.get('token_secret', 'password')
auth_plugin = TokenAuthorizationPlugin(token_secret)
app.install(auth_plugin)

count = AtomicLong(0)
inbox = Inboxer(atomic_counter=count,
                master_inbox_path=app.config.get('inbox.master',
                                                 DEFAULT_MASTER_INBOX),
                container_inboxes_path=app.config.get(
                    'inbox.containers', DEFAULT_CONTAINER_INBOXES))
docker_agent = DockerAgent(registry_host=app.config.get("registry.host", None),
                           username=app.config.get("registry.username", None),
                           password=app.config.get("registry.password", None),
                           docker_timeout=app.config.get("docker.timeout", 2))

orchestrator = Orchestrator(inboxer=inbox, docker_agent=docker_agent)


# Accepts a multipart form
# Field 'topic' -> The topic for the attached file(s)
예제 #11
0
def test_atomiclong_isub():
    l = AtomicLong(0)
    l -= 10
    assert l.value == -10
예제 #12
0
def test_atomiclong_iadd():
    l = AtomicLong(0)
    l += 10
    assert l.value == 10
예제 #13
0
def test_atomiclong_value():
    l = AtomicLong(0)
    assert l.value == 0
    l.value = 10
    assert l.value == 10
예제 #14
0
def test_atomiclong_repr():
    l = AtomicLong(123456789)
    assert '<AtomicLong at ' in repr(l)
    assert '123456789>' in repr(l)