~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-03-20 22:39:38 UTC
  • mfrom: (2363.3.4 simple_locking)
  • Revision ID: pqm@pqm.ubuntu.com-20070320223938-97fdc295a1111e36
(John Arbash Meinel) allow win32 to grab a write lock after 'bzr status', so that we can record the updated stat values.

Show diffs side-by-side

added added

removed removed

Lines of Context:
367
367
        finally:
368
368
            state.unlock()
369
369
 
 
370
    def test_can_save_in_read_lock(self):
 
371
        self.build_tree(['a-file'])
 
372
        state = dirstate.DirState.initialize('dirstate')
 
373
        try:
 
374
            # No stat and no sha1 sum.
 
375
            state.add('a-file', 'a-file-id', 'file', None, '')
 
376
            state.save()
 
377
        finally:
 
378
            state.unlock()
 
379
 
 
380
        # Now open in readonly mode
 
381
        state = dirstate.DirState.on_file('dirstate')
 
382
        state.lock_read()
 
383
        try:
 
384
            entry = state._get_entry(0, path_utf8='a-file')
 
385
            # The current sha1 sum should be empty
 
386
            self.assertEqual('', entry[1][0][1])
 
387
            # We should have a real entry.
 
388
            self.assertNotEqual((None, None), entry)
 
389
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
390
            # We should have gotten a real sha1
 
391
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
392
                             sha1sum)
 
393
 
 
394
            # The dirblock has been updated
 
395
            self.assertEqual(sha1sum, entry[1][0][1])
 
396
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
397
                             state._dirblock_state)
 
398
 
 
399
            del entry
 
400
            # Now, since we are the only one holding a lock, we should be able
 
401
            # to save and have it written to disk
 
402
            state.save()
 
403
        finally:
 
404
            state.unlock()
 
405
 
 
406
        # Re-open the file, and ensure that the state has been updated.
 
407
        state = dirstate.DirState.on_file('dirstate')
 
408
        state.lock_read()
 
409
        try:
 
410
            entry = state._get_entry(0, path_utf8='a-file')
 
411
            self.assertEqual(sha1sum, entry[1][0][1])
 
412
        finally:
 
413
            state.unlock()
 
414
 
 
415
    def test_save_fails_quietly_if_locked(self):
 
416
        """If dirstate is locked, save will fail without complaining."""
 
417
        self.build_tree(['a-file'])
 
418
        state = dirstate.DirState.initialize('dirstate')
 
419
        try:
 
420
            # No stat and no sha1 sum.
 
421
            state.add('a-file', 'a-file-id', 'file', None, '')
 
422
            state.save()
 
423
        finally:
 
424
            state.unlock()
 
425
 
 
426
        state = dirstate.DirState.on_file('dirstate')
 
427
        state.lock_read()
 
428
        try:
 
429
            entry = state._get_entry(0, path_utf8='a-file')
 
430
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
431
            # We should have gotten a real sha1
 
432
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
433
                             sha1sum)
 
434
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
435
                             state._dirblock_state)
 
436
 
 
437
            # Now, before we try to save, grab another dirstate, and take out a
 
438
            # read lock.
 
439
            # TODO: jam 20070315 Ideally this would be locked by another
 
440
            #       process. To make sure the file is really OS locked.
 
441
            state2 = dirstate.DirState.on_file('dirstate')
 
442
            state2.lock_read()
 
443
            try:
 
444
                # This won't actually write anything, because it couldn't grab
 
445
                # a write lock. But it shouldn't raise an error, either.
 
446
                # TODO: jam 20070315 We should probably distinguish between
 
447
                #       being dirty because of 'update_entry'. And dirty
 
448
                #       because of real modification. So that save() *does*
 
449
                #       raise a real error if it fails when we have real
 
450
                #       modifications.
 
451
                state.save()
 
452
            finally:
 
453
                state2.unlock()
 
454
        finally:
 
455
            state.unlock()
 
456
        
 
457
        # The file on disk should not be modified.
 
458
        state = dirstate.DirState.on_file('dirstate')
 
459
        state.lock_read()
 
460
        try:
 
461
            entry = state._get_entry(0, path_utf8='a-file')
 
462
            self.assertEqual('', entry[1][0][1])
 
463
        finally:
 
464
            state.unlock()
 
465
 
370
466
 
371
467
class TestDirStateInitialize(TestCaseWithDirState):
372
468