summaryrefslogtreecommitdiffstats
path: root/karm/test/locking.cpp
blob: a8f32c059cabf3db63dca5d1c8924808d273dac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <assert.h>

#include <tqstring.h>
#include <tqfile.h>
#include <tqdir.h>
#include <tdecmdlineargs.h>
#include <tdeapplication.h>

#include <resourcecalendar.h>
#include <resourcelocal.h>
#include <calendarresources.h>

#include "lockerthread.h"

const TQString icalfilename = "karmtest.ics";

// If one thread has the file is locked, the other cannot get the lock.
short test1()
{
  short rval = 0;

  KCal::CalendarResources         *calendars = 0;
  KCal::ResourceCalendar          *calendar  = 0;
  KCal::CalendarResources::Ticket *lock      = 0;

  calendars = new KCal::CalendarResources( TQString::fromLatin1( "UTC" ) );
  calendar  = new KCal::ResourceLocal( icalfilename );
  lock      = calendars->requestSaveTicket( calendar );

  if ( !lock ) 
  {
    kdDebug( 5970 ) << "test1(): failed to lock " << icalfilename << endl;
    rval = 1;
  }

  if ( !rval )
  {
    LockerThread thread( icalfilename );
    thread.run();
    if ( thread.gotlock() )
    {
      kdDebug( 5970 ) << "test1(): second thread was able to lock " << icalfilename << endl;
      rval = 1;
    }
  }

  // This frees the lock memory.
  calendars->releaseSaveTicket( lock );

  delete calendar;
  delete calendars;

  return rval;
}

// First thread opens but doesn't lock, second should get lock. 
short test2()
{
  short rval = 0;

  KCal::CalendarResources         *calendars = 0;
  KCal::ResourceCalendar          *calendar  = 0;
  KCal::CalendarResources::Ticket *lock      = 0;

  calendars = new KCal::CalendarResources( TQString::fromLatin1( "UTC" ) );
  calendar  = new KCal::ResourceLocal( icalfilename );

  LockerThread thread( icalfilename );
  thread.run();
  if ( !thread.gotlock() )
  {
    kdDebug(5970) << "test2(): second thread was not able to lock " << icalfilename << endl;
    rval = 1;
  }

  delete calendar;
  delete calendars;

  return rval;
}

// First thread locks, then unlocks--second should get lock. 
short test3()
{
  short rval = 0;

  KCal::CalendarResources         *calendars = 0;
  KCal::ResourceCalendar          *calendar  = 0;
  KCal::CalendarResources::Ticket *lock      = 0;

  calendars = new KCal::CalendarResources( TQString::fromLatin1( "UTC" ) );
  calendar  = new KCal::ResourceLocal( icalfilename );

  // lock then unlock
  lock = calendars->requestSaveTicket( calendar );
  if ( !lock ) 
  {
    kdDebug( 5970 ) << "test1(): failed to lock " << icalfilename << endl;
    rval = 1;
  }
  calendars->releaseSaveTicket( lock );

  // second thread should get lock
  if ( !rval )
  {
    LockerThread thread( icalfilename );
    thread.run();
    if ( !thread.gotlock() )
    {
      kdDebug( 5970 ) << "test1(): second thread was not able to lock " << icalfilename << endl;
      rval = 1;
    }
  }


  delete calendar;
  delete calendars;

  return rval;
}

// TODO:
// If one thread changes the file, the other is notified.
// What happens if we lock one incident and try to change another?

int main( int argc, char *argv[] )
{
  short rval = 0;

  // Use another directory than the real one, just to keep things clean
  // TDEHOME needs to be writable though, for a tdesycoca database
  // FIXME: Delete this directory when done with test.
  setenv( "TDEHOME", TQFile::encodeName( TQDir::homeDirPath() + "/.tde-testresource" ), true );

  // Copied from Till's test in libkcal.  Not sure what this is for.
  setenv( "TDE_FORK_SLAVES", "yes", true ); // simpler, for the final cleanup

  // Copied from Till's test in libkcal.  Not sure what this is for.
  TDEApplication::disableAutoDcopRegistration();

  TDECmdLineArgs::init(argc,argv,"testresourcelocking", 0, 0, 0, 0);

  TDEApplication app( false, false );

  // basic libkcal locking stuff
  if ( !rval ) rval = test1();
  if ( !rval ) rval = test2();
  if ( !rval ) rval = test3();

  return rval;
}