• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jcmellado / dartis / 50

pending completion
50

push

travis-ci

web-flow
Added some logic to control broken connections (#16)

Added some logic for handling broken connections.

48 of 48 new or added lines in 9 files covered. (100.0%)

1278 of 1431 relevant lines covered (89.31%)

1.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.1
/lib/src/client/transaction.dart
1
// Copyright (c) 2018, Juan Mellado. All rights reserved. Use of this source
2
// is governed by a MIT-style license that can be found in the LICENSE file.
3

4
import '../command.dart';
5
import '../exception.dart';
6
import '../protocol.dart';
7

8
/// A transaction.
9
class Transaction {
10
  /// Queued commands.
11
  final List<Command<Object>> _queued = [];
12

13
  /// Whether this transaction is in progress.
14
  bool _inProgress = false;
15

16
  /// Returns the "in progress" flag.
17
  bool get inProgress => _inProgress;
2✔
18

19
  /// Starts this transaction.
20
  void begin(Command<Object> command) {
1✔
21
    _inProgress = command is MultiCommand;
2✔
22
  }
23

24
  /// Ends this transaction.
25
  void end() {
1✔
26
    _queued.clear();
2✔
27
    _inProgress = false;
1✔
28
  }
29

30
  /// Completes a [command] with a [reply] in the context of this transaction.
31
  void onReply(Command<Object> command, Reply reply, RedisCodec codec) {
1✔
32
    assert(_inProgress);
33
    assert(command is! MultiCommand);
34

35
    if (command is ExecCommand) {
1✔
36
      _exec(command, reply, codec);
1✔
37
    } else if (command is DiscardCommand) {
1✔
38
      _discard(command, reply, codec);
1✔
39
    } else {
40
      _enqueue(command, reply, codec);
1✔
41
    }
42
  }
43

44
  /// Completes a [command] with an error [reply] in the context of
45
  /// this transaction.
46
  void onErrorReply(
1✔
47
      Command<Object> command, ErrorReply reply, RedisCodec codec) {
48
    assert(_inProgress);
49

50
    // Failed EXEC command?
51
    if (command is ExecCommand) {
1✔
52
      _abort(reply, codec);
1✔
53
    }
54

55
    command.completeErrorReply(reply, codec);
1✔
56
  }
57

58
  /// Completes all commands in the transaction with [error].
59
  void onError(Object error, StackTrace stackTrace) {
×
60
    assert(_inProgress);
61

62
    for (final command in _queued) {
×
63
      command.completeError(error);
×
64
    }
65

66
    end();
×
67
  }
68

69
  /// Completes all the queued commands.
70
  void _exec(Command<Object> command, Reply reply, RedisCodec codec) {
1✔
71
    // Redis server replies a null value when some watched keys are modified.
72
    if (reply.value == null) {
1✔
73
      _discard(command, reply, codec);
1✔
74
    } else {
75
      // ignore: avoid_as
76
      _dequeue(command, reply as ArrayReply, codec);
1✔
77
    }
78
  }
79

80
  /// Completes all the queued commands with an error.
81
  void _discard(Command<Object> command, Reply reply, RedisCodec codec) {
1✔
82
    final error = ErrorReply('Transaction discarded.'.codeUnits);
2✔
83

84
    for (final command in _queued) {
2✔
85
      command.completeErrorReply(error, codec);
1✔
86
    }
87

88
    command.complete(reply, codec);
1✔
89

90
    end();
1✔
91
  }
92

93
  /// Completes all the queued commands with the given error [reply].
94
  void _abort(ErrorReply reply, RedisCodec codec) {
1✔
95
    for (final command in _queued) {
2✔
96
      command.completeErrorReply(reply, codec);
1✔
97
    }
98

99
    end();
1✔
100
  }
101

102
  /// Enqueues a [command].
103
  void _enqueue(Command<Object> command, Reply reply, RedisCodec codec) {
1✔
104
    if (reply is! StringReply) {
1✔
105
      throw RedisException(
1✔
106
          'Expected "StringReply", but "${reply.runtimeType}" found instead.');
2✔
107
    }
108

109
    final value = codec.decode<String>(reply);
1✔
110
    if (value != 'QUEUED') {
1✔
111
      throw RedisException(
1✔
112
          'Expected "QUEUED" reply, but "$value" found instead.');
1✔
113
    }
114

115
    // Enqueue the command.
116
    _queued.add(command);
2✔
117
  }
118

119
  /// Completes the queued commands with the array of replies in [reply].
120
  void _dequeue(Command<Object> command, ArrayReply reply, RedisCodec codec) {
1✔
121
    final array = reply.array;
1✔
122

123
    if (array.length != _queued.length) {
4✔
124
      throw RedisException('''Expected ${_queued.length} replies,'''
3✔
125
          ''' but "${array.length}" found instead.''');
1✔
126
    }
127

128
    // Completes the queued commands.
129
    _dequeueAll(array, codec);
1✔
130

131
    command.complete(reply, codec);
1✔
132

133
    end();
1✔
134
  }
135

136
  /// Completes the queued commands.
137
  void _dequeueAll(List<Reply> array, RedisCodec codec) {
1✔
138
    for (var i = 0; i < _queued.length; i++) {
4✔
139
      final command = _queued[i];
2✔
140
      final reply = array[i];
1✔
141

142
      if (reply is ErrorReply) {
1✔
143
        command.completeErrorReply(reply, codec);
1✔
144
      } else {
145
        command.complete(reply, codec);
1✔
146
      }
147
    }
148
  }
149
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc