hydrus/test.py

369 lines
11 KiB
Python
Raw Normal View History

2015-11-25 22:00:57 +00:00
#!/usr/bin/env python2
2014-07-30 21:18:17 +00:00
import locale
try: locale.setlocale( locale.LC_ALL, '' )
except: pass
2013-07-10 20:25:57 +00:00
from include import HydrusConstants as HC
2014-04-23 20:56:12 +00:00
from include import ClientConstants as CC
2015-03-25 22:04:19 +00:00
from include import HydrusGlobals
2015-03-18 21:46:29 +00:00
from include import ClientDefaults
2015-10-21 21:53:10 +00:00
from include import ClientNetworking
2015-08-26 21:18:39 +00:00
from include import HydrusPubSub
2013-10-23 21:36:47 +00:00
from include import HydrusSessions
2013-07-24 20:26:00 +00:00
from include import HydrusTags
2015-08-26 21:18:39 +00:00
from include import HydrusThreading
2013-08-07 22:25:18 +00:00
from include import TestClientConstants
2013-07-31 21:26:38 +00:00
from include import TestClientDaemons
2015-04-01 20:44:54 +00:00
from include import TestClientDownloading
2013-07-10 20:25:57 +00:00
from include import TestConstants
2013-07-24 20:26:00 +00:00
from include import TestDialogs
2013-07-10 20:25:57 +00:00
from include import TestDB
from include import TestFunctions
2015-11-18 22:44:07 +00:00
from include import TestClientImageHandling
2014-04-16 20:31:59 +00:00
from include import TestHydrusNATPunch
2013-11-20 21:12:21 +00:00
from include import TestHydrusServer
2013-11-06 18:22:07 +00:00
from include import TestHydrusSessions
2013-07-17 20:56:13 +00:00
from include import TestHydrusTags
2013-07-31 21:26:38 +00:00
import collections
2013-07-24 20:26:00 +00:00
import os
2015-08-26 21:18:39 +00:00
import random
2015-12-02 22:32:18 +00:00
import shutil
2013-11-06 18:22:07 +00:00
import sys
2015-12-02 22:32:18 +00:00
import tempfile
2013-11-27 18:27:11 +00:00
import threading
2014-04-16 20:31:59 +00:00
import time
2013-07-10 20:25:57 +00:00
import unittest
2013-07-24 20:26:00 +00:00
import wx
2013-11-27 18:27:11 +00:00
from twisted.internet import reactor
2015-03-18 21:46:29 +00:00
from include import ClientCaches
from include import ClientData
2015-03-25 22:04:19 +00:00
from include import HydrusData
2016-08-17 20:07:22 +00:00
from include import HydrusPaths
2013-07-10 20:25:57 +00:00
2013-11-06 18:22:07 +00:00
only_run = None
2015-09-02 23:16:09 +00:00
class Controller( object ):
2013-07-24 20:26:00 +00:00
2015-09-02 23:16:09 +00:00
def __init__( self ):
2013-07-24 20:26:00 +00:00
2016-10-12 21:52:50 +00:00
self._db_dir = tempfile.mkdtemp()
2016-02-03 22:12:53 +00:00
2016-10-12 21:52:50 +00:00
TestConstants.DB_DIR = self._db_dir
2016-02-03 22:12:53 +00:00
2016-10-12 21:52:50 +00:00
self._server_files_dir = os.path.join( self._db_dir, 'server_files' )
self._updates_dir = os.path.join( self._db_dir, 'test_updates' )
2016-02-03 22:12:53 +00:00
2016-10-12 21:52:50 +00:00
client_files_default = os.path.join( self._db_dir, 'client_files' )
2016-06-01 20:04:15 +00:00
2016-08-17 20:07:22 +00:00
HydrusPaths.MakeSureDirectoryExists( client_files_default )
2016-02-17 22:06:47 +00:00
2015-08-26 21:18:39 +00:00
HydrusGlobals.controller = self
2015-09-16 18:11:00 +00:00
HydrusGlobals.client_controller = self
HydrusGlobals.server_controller = self
HydrusGlobals.test_controller = self
2015-08-26 21:18:39 +00:00
2016-02-03 22:12:53 +00:00
self._pubsub = HydrusPubSub.HydrusPubSub( self )
2015-12-02 22:32:18 +00:00
2016-10-12 21:52:50 +00:00
self._new_options = ClientData.ClientOptions( self._db_dir )
2015-10-14 21:02:25 +00:00
2014-03-26 21:23:10 +00:00
def show_text( text ): pass
2015-03-25 22:04:19 +00:00
HydrusData.ShowText = show_text
2014-03-26 21:23:10 +00:00
2015-10-21 21:53:10 +00:00
self._http = ClientNetworking.HTTPConnectionManager()
2015-04-01 20:44:54 +00:00
2016-07-20 19:57:10 +00:00
self._call_to_threads = []
2013-07-24 20:26:00 +00:00
self._reads = {}
2013-11-27 18:27:11 +00:00
self._reads[ 'hydrus_sessions' ] = []
2014-07-16 20:50:18 +00:00
self._reads[ 'local_booru_share_keys' ] = []
2013-11-27 18:27:11 +00:00
self._reads[ 'messaging_sessions' ] = []
2014-03-12 22:08:23 +00:00
self._reads[ 'tag_censorship' ] = []
2015-03-18 21:46:29 +00:00
self._reads[ 'options' ] = ClientDefaults.GetClientDefaultOptions()
2014-08-13 22:18:12 +00:00
services = []
2016-01-20 23:57:33 +00:00
services.append( ClientData.GenerateService( CC.LOCAL_BOORU_SERVICE_KEY, HC.LOCAL_BOORU, CC.LOCAL_BOORU_SERVICE_KEY, { 'max_monthly_data' : None, 'used_monthly_data' : 0 } ) )
services.append( ClientData.GenerateService( CC.LOCAL_FILE_SERVICE_KEY, HC.LOCAL_FILE, CC.LOCAL_FILE_SERVICE_KEY, {} ) )
services.append( ClientData.GenerateService( CC.LOCAL_TAG_SERVICE_KEY, HC.LOCAL_TAG, CC.LOCAL_TAG_SERVICE_KEY, {} ) )
2014-08-13 22:18:12 +00:00
self._reads[ 'services' ] = services
2016-07-27 21:53:34 +00:00
client_files_locations = {}
for prefix in HydrusData.IterateHexPrefixes():
for c in ( 'f', 't', 'r' ):
client_files_locations[ c + prefix ] = client_files_default
2015-12-02 22:32:18 +00:00
self._reads[ 'client_files_locations' ] = client_files_locations
2013-11-27 18:27:11 +00:00
self._reads[ 'sessions' ] = []
2013-07-24 20:26:00 +00:00
self._reads[ 'tag_parents' ] = {}
self._reads[ 'tag_siblings' ] = {}
2013-11-27 18:27:11 +00:00
self._reads[ 'web_sessions' ] = {}
2013-07-24 20:26:00 +00:00
2015-03-18 21:46:29 +00:00
HC.options = ClientDefaults.GetClientDefaultOptions()
2013-09-11 21:28:19 +00:00
2013-07-31 21:26:38 +00:00
self._writes = collections.defaultdict( list )
2013-11-27 18:27:11 +00:00
self._managers = {}
2013-07-24 20:26:00 +00:00
2015-11-25 22:00:57 +00:00
self._services_manager = ClientCaches.ServicesManager( self )
2015-12-02 22:32:18 +00:00
self._client_files_manager = ClientCaches.ClientFilesManager( self )
2015-12-09 23:16:41 +00:00
self._client_session_manager = ClientCaches.HydrusSessionManager( self )
2014-08-13 22:18:12 +00:00
2015-11-25 22:00:57 +00:00
self._managers[ 'tag_censorship' ] = ClientCaches.TagCensorshipManager( self )
self._managers[ 'tag_siblings' ] = ClientCaches.TagSiblingsManager( self )
self._managers[ 'tag_parents' ] = ClientCaches.TagParentsManager( self )
self._managers[ 'undo' ] = ClientCaches.UndoManager( self )
2013-12-04 22:44:16 +00:00
self._managers[ 'web_sessions' ] = TestConstants.FakeWebSessionManager()
2015-12-09 23:16:41 +00:00
self._server_session_manager = HydrusSessions.HydrusSessionManagerServer()
2015-11-25 22:00:57 +00:00
self._managers[ 'local_booru' ] = ClientCaches.LocalBooruCache( self )
2014-07-16 20:50:18 +00:00
2013-09-04 16:48:44 +00:00
self._cookies = {}
2013-07-10 20:25:57 +00:00
2016-07-20 19:57:10 +00:00
def _GetCallToThread( self ):
for call_to_thread in self._call_to_threads:
if not call_to_thread.CurrentlyWorking():
return call_to_thread
if len( self._call_to_threads ) > 100:
raise Exception( 'Too many call to threads!' )
call_to_thread = HydrusThreading.THREADCallToThread( self )
self._call_to_threads.append( call_to_thread )
call_to_thread.start()
return call_to_thread
2015-08-26 21:18:39 +00:00
def pub( self, topic, *args, **kwargs ):
pass
def pubimmediate( self, topic, *args, **kwargs ):
self._pubsub.pubimmediate( topic, *args, **kwargs )
def sub( self, object, method_name, topic ):
self._pubsub.sub( object, method_name, topic )
def CallToThread( self, callable, *args, **kwargs ):
2016-07-20 19:57:10 +00:00
call_to_thread = self._GetCallToThread()
2015-08-26 21:18:39 +00:00
call_to_thread.put( callable, *args, **kwargs )
2015-04-01 20:44:54 +00:00
def DoHTTP( self, *args, **kwargs ): return self._http.Request( *args, **kwargs )
2015-12-02 22:32:18 +00:00
def GetClientFilesManager( self ):
return self._client_files_manager
2015-12-09 23:16:41 +00:00
def GetClientSessionManager( self ):
return self._client_session_manager
2016-10-12 21:52:50 +00:00
def GetFilesDir( self ):
return self._server_files_dir
2015-04-01 20:44:54 +00:00
def GetHTTP( self ): return self._http
2015-10-14 21:02:25 +00:00
def GetNewOptions( self ):
return self._new_options
2015-05-13 20:22:39 +00:00
def GetOptions( self ):
return HC.options
2014-09-10 22:37:38 +00:00
def GetManager( self, manager_type ): return self._managers[ manager_type ]
2013-07-10 20:25:57 +00:00
2015-07-01 22:02:07 +00:00
def GetServicesManager( self ):
return self._services_manager
2015-12-09 23:16:41 +00:00
def GetServerSessionManager( self ):
return self._server_session_manager
2016-10-12 21:52:50 +00:00
def GetUpdatesDir( self ):
return self._updates_dir
2013-07-31 21:26:38 +00:00
def GetWrite( self, name ):
write = self._writes[ name ]
del self._writes[ name ]
return write
2016-08-31 19:55:14 +00:00
def IsFirstStart( self ):
return True
2015-11-04 22:30:28 +00:00
def ModelIsShutdown( self ):
return HydrusGlobals.model_shutdown
2013-10-23 21:36:47 +00:00
def Read( self, name, *args, **kwargs ): return self._reads[ name ]
2013-07-10 20:25:57 +00:00
2014-12-03 22:56:40 +00:00
def ResetIdleTimer( self ): pass
2015-09-02 23:16:09 +00:00
def Run( self ):
suites = []
if only_run is None: run_all = True
else: run_all = False
if run_all or only_run == 'cc': suites.append( unittest.TestLoader().loadTestsFromModule( TestClientConstants ) )
if run_all or only_run == 'daemons': suites.append( unittest.TestLoader().loadTestsFromModule( TestClientDaemons ) )
if run_all or only_run == 'dialogs': suites.append( unittest.TestLoader().loadTestsFromModule( TestDialogs ) )
if run_all or only_run == 'db': suites.append( unittest.TestLoader().loadTestsFromModule( TestDB ) )
if run_all or only_run == 'downloading': suites.append( unittest.TestLoader().loadTestsFromModule( TestClientDownloading ) )
if run_all or only_run == 'functions': suites.append( unittest.TestLoader().loadTestsFromModule( TestFunctions ) )
2015-11-18 22:44:07 +00:00
if run_all or only_run == 'image': suites.append( unittest.TestLoader().loadTestsFromModule( TestClientImageHandling ) )
2015-09-02 23:16:09 +00:00
if run_all or only_run == 'nat': suites.append( unittest.TestLoader().loadTestsFromModule( TestHydrusNATPunch ) )
if run_all or only_run == 'server': suites.append( unittest.TestLoader().loadTestsFromModule( TestHydrusServer ) )
if run_all or only_run == 'sessions': suites.append( unittest.TestLoader().loadTestsFromModule( TestHydrusSessions ) )
if run_all or only_run == 'tags': suites.append( unittest.TestLoader().loadTestsFromModule( TestHydrusTags ) )
suite = unittest.TestSuite( suites )
runner = unittest.TextTestRunner( verbosity = 1 )
runner.run( suite )
2015-04-01 20:44:54 +00:00
def SetHTTP( self, http ): self._http = http
2013-07-24 20:26:00 +00:00
def SetRead( self, name, value ): self._reads[ name ] = value
2013-07-10 20:25:57 +00:00
2013-09-04 16:48:44 +00:00
def SetWebCookies( self, name, value ): self._cookies[ name ] = value
2015-12-02 22:32:18 +00:00
def TidyUp( self ):
2016-10-12 21:52:50 +00:00
shutil.rmtree( self._db_dir )
2015-12-02 22:32:18 +00:00
2015-11-04 22:30:28 +00:00
def ViewIsShutdown( self ):
return HydrusGlobals.view_shutdown
2013-07-31 21:26:38 +00:00
def Write( self, name, *args, **kwargs ):
self._writes[ name ].append( ( args, kwargs ) )
2013-08-14 20:21:49 +00:00
def WriteSynchronous( self, name, *args, **kwargs ):
2013-07-31 21:26:38 +00:00
self._writes[ name ].append( ( args, kwargs ) )
if name == 'import_file':
2013-08-07 22:25:18 +00:00
( path, ) = args
with open( path, 'rb' ) as f: file = f.read()
if file == 'blarg': raise Exception( 'File failed to import for some reason!' )
2015-09-09 22:04:39 +00:00
else: return ( CC.STATUS_SUCCESSFUL, '0123456789abcdef'.decode( 'hex' ) )
2013-07-31 21:26:38 +00:00
2013-07-24 20:26:00 +00:00
if __name__ == '__main__':
2013-07-10 20:25:57 +00:00
2013-11-06 18:22:07 +00:00
args = sys.argv[1:]
if len( args ) > 0:
only_run = args[0]
else: only_run = None
2015-08-26 21:18:39 +00:00
try:
threading.Thread( target = reactor.run, kwargs = { 'installSignalHandlers' : 0 } ).start()
2015-09-02 23:16:09 +00:00
app = wx.App()
2016-06-08 20:27:22 +00:00
controller = Controller()
2015-09-02 23:16:09 +00:00
2016-06-08 20:27:22 +00:00
try:
win = wx.Frame( None )
wx.CallAfter( controller.Run )
#threading.Thread( target = controller.Run ).start()
wx.CallAfter( win.Destroy )
app.MainLoop()
except:
import traceback
traceback.print_exc()
finally:
HydrusGlobals.view_shutdown = True
controller.pubimmediate( 'wake_daemons' )
HydrusGlobals.model_shutdown = True
controller.pubimmediate( 'wake_daemons' )
2016-10-12 21:52:50 +00:00
controller.TidyUp()
2016-06-08 20:27:22 +00:00
2015-08-26 21:18:39 +00:00
2015-09-16 18:11:00 +00:00
except:
import traceback
traceback.print_exc()
2015-08-26 21:18:39 +00:00
finally:
reactor.callFromThread( reactor.stop )
2016-06-08 20:27:22 +00:00
print( 'This was version ' + str( HC.SOFTWARE_VERSION ) )
2015-08-26 21:18:39 +00:00
2016-06-08 20:27:22 +00:00
raw_input()