#!/usr/share/ucs-test/runner python3
## -*- coding: utf-8 -*-
## desc: test automatic reconnect of uldap.py
## tags: [skip_admember,reconnect]
## roles: [domaincontroller_master]
## exposure: dangerous
## packages:
##   - python-univention-directory-manager
##   - python-univention

from __future__ import print_function
import time
import threading
import subprocess
from ldap import LDAPError

from univention.uldap import getMachineConnection

failed = False
run_time = 10.0


def search_thread():
	global failed
	lo = getMachineConnection()
	lo.lo._retry_max = 10E4
	lo.lo._retry_delay = .001
	x = 0
	lo.search(filter="uid=Administrator", attr=["uid"])
	s = time.time()
	print("go")
	while (time.time() - s) < run_time:
		x += 1
		try:
			lo.search(filter="uid=Administrator", attr=["uid"])
		except LDAPError:
			failed = True
	print("Searches per sec: {}".format(x / run_time))


def main():
	thread_count = 100
	my_thread = [None] * thread_count
	for i in range(0, thread_count):
		my_thread[i] = threading.Thread(target=search_thread)
	for t in my_thread:
		t.start()

	print("warmup")
	time.sleep(3)

	print("restart slapd")
	subprocess.check_call(["systemctl", "restart", "slapd.service"])
	print("restarted")

	for t in my_thread:
		t.join()

	print("done")
	assert not failed


if __name__ == '__main__':
	try:
		main()
	finally:
		subprocess.check_call(["systemctl", "restart", "slapd.service"])
