Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
engine
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AURA
engine
Commits
aa076ad3
Commit
aa076ad3
authored
2 years ago
by
David Trattnig
Browse files
Options
Downloads
Patches
Plain Diff
test(decorator): synchronized
parent
032ea0ac
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/test_synchronized.py
+182
-0
182 additions, 0 deletions
tests/test_synchronized.py
with
182 additions
and
0 deletions
tests/test_synchronized.py
0 → 100644
+
182
−
0
View file @
aa076ad3
#
# Aura Engine (https://gitlab.servus.at/aura/engine)
#
# Copyright (C) 2017-now() - The Aura Engine Team.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import
threading
import
time
import
unittest
from
aura_engine.base.lang
import
synchronized
class
MessageHolder
:
message
=
None
def
__init__
(
self
):
self
.
message
=
"
no message
"
def
set_message
(
self
,
message
:
str
,
sleep_time
:
float
):
print
(
f
"
Updating message to
'
{
message
}
'
in
{
sleep_time
}
seconds
"
)
time
.
sleep
(
sleep_time
)
self
.
message
=
message
# print("Message updated")
@synchronized
def
set_synced_message
(
self
,
message
:
str
,
sleep_time
:
float
):
print
(
f
"
Synced: Updating message to
'
{
message
}
'
in
{
sleep_time
}
seconds
"
)
time
.
sleep
(
sleep_time
)
self
.
message
=
message
def
get_message
(
self
):
return
self
.
message
class
TestSynchronized
(
unittest
.
TestCase
):
"""
Testing the Logger.
"""
mh
=
None
def
setUp
(
self
):
self
.
mh
=
MessageHolder
()
def
test_not_synchronized
(
self
):
# Functions to update the message via threads
def
fast_cat_1
():
self
.
mh
.
set_message
(
"
fast cat 1
"
,
0.05
)
def
sleepy_dog_1
():
self
.
mh
.
set_message
(
"
sleepy dog 1
"
,
0.5
)
def
fast_cat_2
():
self
.
mh
.
set_message
(
"
fast cat 2
"
,
0.1
)
def
sleepy_dog_2
():
self
.
mh
.
set_message
(
"
sleepy dog 2
"
,
1
)
# CASE#0: Get initial message
msg
=
self
.
mh
.
get_message
()
print
(
msg
)
self
.
assertEqual
(
"
no message
"
,
msg
)
# Start threads
thread1
=
threading
.
Thread
(
target
=
fast_cat_1
)
thread2
=
threading
.
Thread
(
target
=
sleepy_dog_1
)
thread3
=
threading
.
Thread
(
target
=
sleepy_dog_2
)
thread4
=
threading
.
Thread
(
target
=
fast_cat_2
)
thread1
.
start
()
thread2
.
start
()
thread3
.
start
()
thread4
.
start
()
# CASE#1: First thread quickly updates the message
time
.
sleep
(
0.08
)
msg
=
self
.
mh
.
get_message
()
print
(
msg
)
self
.
assertEqual
(
"
fast cat 1
"
,
msg
)
# CASE#2: Last thread has overtaken the two slow ones
time
.
sleep
(
0.12
)
msg
=
self
.
mh
.
get_message
()
print
(
msg
)
self
.
assertEqual
(
"
fast cat 2
"
,
msg
)
# # CASE#3: Slow one arrived
time
.
sleep
(
0.5
)
msg
=
self
.
mh
.
get_message
()
print
(
msg
)
self
.
assertEqual
(
"
sleepy dog 1
"
,
msg
)
# # CASE#3: The other slow one arrived
time
.
sleep
(
0.5
)
msg
=
self
.
mh
.
get_message
()
print
(
msg
)
self
.
assertEqual
(
"
sleepy dog 2
"
,
msg
)
thread1
.
join
()
thread2
.
join
()
thread3
.
join
()
thread4
.
join
()
def
test_synchronized
(
self
):
# Functions to update the message via threads
def
fast_cat_1
():
self
.
mh
.
set_synced_message
(
"
fast cat 1
"
,
0.05
)
def
sleepy_dog_1
():
self
.
mh
.
set_synced_message
(
"
sleepy dog 1
"
,
0.5
)
def
fast_cat_2
():
self
.
mh
.
set_synced_message
(
"
fast cat 2
"
,
0.1
)
def
sleepy_dog_2
():
self
.
mh
.
set_synced_message
(
"
sleepy dog 2
"
,
1
)
# CASE#0: Get initial message
msg
=
self
.
mh
.
get_message
()
print
(
msg
)
self
.
assertEqual
(
"
no message
"
,
msg
)
# Start threads
thread1
=
threading
.
Thread
(
target
=
fast_cat_1
)
thread2
=
threading
.
Thread
(
target
=
sleepy_dog_1
)
thread3
=
threading
.
Thread
(
target
=
sleepy_dog_2
)
thread4
=
threading
.
Thread
(
target
=
fast_cat_2
)
thread1
.
start
()
time
.
sleep
(
0.01
)
thread2
.
start
()
time
.
sleep
(
0.01
)
thread3
.
start
()
time
.
sleep
(
0.01
)
thread4
.
start
()
# CASE#1: First thread quickly updates the message
time
.
sleep
(
0.1
)
msg
=
self
.
mh
.
get_message
()
print
(
msg
)
self
.
assertEqual
(
"
fast cat 1
"
,
msg
)
# # CASE#2: Any fast cat has to wait for this dog
time
.
sleep
(
0.5
)
msg
=
self
.
mh
.
get_message
()
print
(
msg
)
self
.
assertEqual
(
"
sleepy dog 1
"
,
msg
)
# # CASE#3: And for the other dog too
time
.
sleep
(
1
)
msg
=
self
.
mh
.
get_message
()
print
(
msg
)
self
.
assertEqual
(
"
sleepy dog 2
"
,
msg
)
# # CASE#3: Finally it's the fast cats turn
time
.
sleep
(
0.2
)
msg
=
self
.
mh
.
get_message
()
print
(
msg
)
self
.
assertEqual
(
"
fast cat 2
"
,
msg
)
thread1
.
join
()
thread2
.
join
()
thread3
.
join
()
thread4
.
join
()
if
__name__
==
"
__main__
"
:
unittest
.
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment