Skip to content

Commit a9718cc

Browse files
authored
[analytics] disable in Makefile and GHA tests, unit test to always check send_event (#293)
1 parent 5d68384 commit a9718cc

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

.github/workflows/test-check.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
runs-on: ubuntu-latest
3535
env:
3636
SPARSEZOO_TEST_MODE: "true"
37+
NM_DISABLE_ANALYTICS: "true"
3738
needs: test-setup
3839
if: ${{needs.test-setup.outputs.python-diff == 1}}
3940
steps:
@@ -46,6 +47,7 @@ jobs:
4647
runs-on: ubuntu-latest
4748
env:
4849
SPARSEZOO_TEST_MODE: "true"
50+
NM_DISABLE_ANALYTICS: "true"
4951
needs: test-setup
5052
if: ${{needs.test-setup.outputs.full-check == 1}}
5153
steps:

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ style:
5353
# run tests for the repo
5454
test:
5555
@echo "Running python tests";
56-
SPARSEZOO_TEST_MODE="true" pytest tests $(PYTEST_ARGS);
56+
SPARSEZOO_TEST_MODE="true" NM_DISABLE_ANALYTICS="true" pytest tests $(PYTEST_ARGS);
5757

5858
# create docs
5959
docs:

src/sparsezoo/analytics.py

+10
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,22 @@ def send_event(
106106
event_name: str,
107107
event_params: Optional[Dict[str, str]] = None,
108108
raise_errors: bool = False,
109+
_await_response: bool = False,
109110
):
110111
"""
111112
Send an event
112113
113114
:param event_name: the name of the event to send
114115
:param event_params: optional dictionary of parameters to send with the event
115116
:param raise_errors: True to raise any errors that occur, False otherwise
117+
:param _await_response: (testing only) True to wait for analytics request
118+
thread to complete before returning. Default False
116119
"""
117120
if self._disabled:
121+
if _await_response:
122+
raise ValueError(
123+
"_await_response cannot be set to True when analytics is disabled"
124+
)
118125
return
119126

120127
if not event_params:
@@ -149,6 +156,9 @@ def _send_request():
149156
thread = threading.Thread(target=_send_request)
150157
thread.start()
151158

159+
if _await_response:
160+
thread.join()
161+
152162

153163
# analytics client for sparsezoo, to disable set NM_DISABLE_ANALYTICS=1
154164
sparsezoo_analytics = GoogleAnalytics("sparsezoo", sparsezoo_version)

tests/sparsezoo/test_analytics.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from copy import deepcopy
16+
17+
from sparsezoo.analytics import sparsezoo_analytics
18+
19+
20+
def test_send_event_success():
21+
test_analytics = deepcopy(sparsezoo_analytics)
22+
test_analytics._disabled = False
23+
24+
test_analytics.send_event(
25+
"tests__analytics__test_send_event_success",
26+
raise_errors=True,
27+
_await_response=True,
28+
)

0 commit comments

Comments
 (0)